Powershell:如何使用存储的非明文用户名/密码连接到不同域上的网络文件夹
我正在尝试通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我用于加密/解密字符串的两个函数。它们改编自 powershell.com 的帖子,但我手边没有链接。这个想法是,您的密码文件将存储 Protect-String 的输出,然后转换回常规字符串,读入文件并调用 UnProtect-String,从 UnProtect string 返回的值将是您的 $password 变量。
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.