Powershell:如何使用存储的非明文用户名/密码连接到不同域上的网络文件夹

发布于 2024-10-11 08:19:46 字数 585 浏览 2 评论 0原文

我正在尝试通过 powershell 连接到网络共享。网络共享位于不同的域中,因此我需要提供凭据。由于 New-PSDrive 不支持凭据,我打算使用 net use,但我担心将我的用户名/密码以明文形式放入脚本中。我强制 ConvertTo-SecureString 从我的密码中生成安全字符串,然后使用 ConvertFrom-SecureString 并将结果存储在文件中。然后,我像这样将其从文件中取出并尝试net use

$password = Get-Content <locationOfStoredPasswordFile> | ConvertTo-SecureString
net use q: "\\server\share" $password /user:domain\username

但是net use无法识别安全字符串。

有人对如何存储凭据以便net use可以使用它们有任何想法吗?谢谢!

I'm trying to connect to a network share via powershell. The network share is on a different domain, so I need to supply credentials. Since New-PSDrive doesn't support credentials, I was going to use net use, but I'm worried about putting my username/password right there in plaintext into the script. I forced ConvertTo-SecureString to make a secure string out of my password and then used ConvertFrom-SecureString and stored the result in a file. Then, I pulled it out of the file like this and tried net use:

$password = Get-Content <locationOfStoredPasswordFile> | ConvertTo-SecureString
net use q: "\\server\share" $password /user:domain\username

But net use doesn't recognize the secure string.

Anyone have any ideas on how to store the credentials so net use can use them? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

り繁华旳梦境 2024-10-18 08:19:46

这是我用于加密/解密字符串的两个函数。它们改编自 powershell.com 的帖子,但我手边没有链接。这个想法是,您的密码文件将存储 Protect-String 的输出,然后转换回常规字符串,读入文件并调用 UnProtect-String,从 UnProtect string 返回的值将是您的 $password 变量。

#######################
function Protect-String 
{
    param([string]$InputString)
    $secure = ConvertTo-SecureString $InputString -asPlainText -force
    $export = $secure | ConvertFrom-SecureString
    write-output $export

} #Protect-String

#######################
function UnProtect-String 
{
    param([string]$InputString)

    $secure = ConvertTo-SecureString $InputString
    $helper = New-Object system.Management.Automation.PSCredential("SQLPSX", $secure)
    $plain = $helper.GetNetworkCredential().Password
    write-output $plain

} #UnProtect-String

Here's two functions I use for encrypting/decrypting strings. They were adapted from a powershell.com post, but I don't have the link handy. The idea is that your password file would store the output of Protect-String and then to convert back to a regular string, read in file and call UnProtect-String, that value returned from UnProtect string would be your $password variable.

#######################
function Protect-String 
{
    param([string]$InputString)
    $secure = ConvertTo-SecureString $InputString -asPlainText -force
    $export = $secure | ConvertFrom-SecureString
    write-output $export

} #Protect-String

#######################
function UnProtect-String 
{
    param([string]$InputString)

    $secure = ConvertTo-SecureString $InputString
    $helper = New-Object system.Management.Automation.PSCredential("SQLPSX", $secure)
    $plain = $helper.GetNetworkCredential().Password
    write-output $plain

} #UnProtect-String
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文