2013-05-07 116 views
2

我需要將憑證存儲在PowerShell中才能使用多次。這裏StackOverflow上也有很多例子,所以我花了一從服務器拒絕的powershell憑證

$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

我看不出什麼明顯的毛病你的代碼,我猜這只是你如何使用它的一個例子,因爲你提到你需要在幾個地方使用它。只是爲了檢查,這確實是安全字符串的失敗,你可以請用以下的,應證明該證書被保存到磁盤之前工作過的存儲:

Get-ADUser -Credential $tmpCred 

一個選擇是繞過憑據而不是一個文件或安全字符串,使用[System.Management.Automation.PSCredential]這個類型,它是從您的調用返回到Get-Credentials並存儲在變量$tmpCred中。

您也暫時將呼叫添加到方法GetNetworkCredentials(),以確保您的密碼已被正確解密,以下將顯示用戶名和密碼(未加密):

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

希望幫助...