【发布时间】:2011-08-18 15:24:56
【问题描述】:
我正在执行本文中描述的操作,将凭据保存在安全文件中,以便我们的自动化进程可以使用它通过 Invoke-command 运行远程 PS 脚本: http://blogs.technet.com/b/robcost/archive/2008/05/01/powershell-tip-storing-and-using-password-credentials.aspx
当我在我的帐户下运行它时效果很好 - 从加密文件中读取密码,传递给 Invoke-command,一切都很好。
今天,当我的脚本准备好进入黄金时段时,我尝试在将由自动化进程使用的 Windows 帐户下运行它,并在我的脚本尝试从文件中读取安全密码时出现以下错误:
ConvertTo-SecureString : Key not valid for use in specified state.
At \\remoted\script.ps1:210 char:87
+ $password = get-content $PathToFolderWithCredentials\pass.txt | convertto-sec
urestring <<<<
+ CategoryInfo : InvalidArgument: (:) [ConvertTo-SecureString], C
ryptographicException
+ FullyQualifiedErrorId : ImportSecureString_InvalidArgument_Cryptographic
Error,Microsoft.PowerShell.Commands.ConvertToSecureStringCommand
让我的同事在他的帐户下运行,他得到了同样的错误。
这是我用来保存凭据的代码:
$PathToFolderWithCredentials = "\\path\removed"
write-host "Enter login as domain\login:"
read-host | out-file $PathToFolderWithCredentials\login.txt
write-host "Enter password:"
read-host -assecurestring | convertfrom-securestring | out-file $PathToFolderWithCredentials\pass.txt
write-host "*** Credentials have been saved to $pathtofolder ***"
这是脚本中的代码,由自动化进程运行以读取它们以在 Invoke-command 中使用:
$login= get-content $PathToFolderWithCredentials\login.txt
$password = get-content $PathToFolderWithCredentials\pass.txt | convertto-securestring
$credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $login,$password
错误发生在 $password = get-content $PathToFolderWithCredentials\pass.txt |转换为安全字符串
有什么想法吗?
【问题讨论】:
-
我读到:“ConvertFrom-SecureString cmdlet 使用 Windows 标准数据保护 API 加密此数据。这确保只有您的用户帐户才能正确解密其内容”。这就是为什么它不起作用...任何想法是保存密码的最佳方法,然后可以由另一个 Windows 帐户解密?
-
发现这篇博文也很有帮助powertoe.wordpress.com/2011/06/05/…
-
我还建议您阅读“Powershell Cookbook” - 适合初学者和高级用户的 goob 书。它涵盖了这一点以及许多其他内容。