3

我需要将凭证存储在 powershell 中以便多次使用。这里 StackOverflow 上有很多例子,所以我拿了一个

$tmpCred = Get-Credential
$tmpCred.Password | ConvertFrom-SecureString | Set-Content "pwd.dat"
$password = Get-Content "pwd.dat" | ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PsCredential "myDomain\myUser", $password

Get-ADUser -Credential $credential 

不幸的是,我收到了这个错误,我找不到解决方案

Get-ADUser : The server has rejected the client credentials.
At line:5 char:11
+ Get-ADUser <<<<  "xxx" -Credential $credential 
    + CategoryInfo          : NotSpecified: (xxx:ADUser) [Get-ADUser], AuthenticationException
    + FullyQualifiedErrorId : The server has rejected the client credentials.,Microsoft.ActiveDirectory.Management.Commands.GetADUser
4

1 回答 1

4

我看不出您的代码有任何明显错误,我猜这只是您如何使用它的一个示例,因为您提到需要在多个地方使用它。只是为了检查它是否真的是安全字符串的存储失败,您可以使用以下内容进行检查,这应该证明凭据在持久保存到磁盘之前有效:

Get-ADUser -Credential $tmpCred 

一种选择是传递凭证而不是文件或安全字符串,使用[System.Management.Automation.PSCredential]从您对 Get-Credentials 的调用返回并存储在变量中的类型$tmpCred

您也可以临时添加对该方法的调用以GetNetworkCredentials()确保您的密码已被正确解密,以下将显示用户名和密码(未加密):

$tmpCred.GetNetworkCredential().Username 
$tmpCred.GetNetworkCredential().Password 

希望有帮助...

于 2013-05-08T09:22:57.830 回答