Dot-Net

創建沒有密碼的 PSCredential

  • July 27, 2011

如何創建沒有密碼的 PSCredential 實例?(無需手動填寫Get-Credential沒有密碼的對話框,這是用於無人值守的執行。)

我嘗試過的事情:

  1. $mycreds = New-Object System.Management.Automation.PSCredential ("username", $null) 錯誤:無法處理參數,因為參數“密碼”的值為空
  2. $mycreds = New-Object System.Management.Automation.PSCredential ("username", (ConvertTo-SecureString $null -AsPlainText -Force)) 錯誤:ConvertTo-SecureString:無法將參數綁定到參數“String”,因為它為空。
  3. $mycreds = New-Object System.Management.Automation.PSCredential ("username", (ConvertTo-SecureString "" -AsPlainText -Force)) 錯誤:ConvertTo-SecureString:無法將參數綁定到參數“String”,因為它是一個空字元串。

解決方案:

$mycreds = New-Object System.Management.Automation.PSCredential 
             ("username", (new-object System.Security.SecureString))

引用自:https://stackoverflow.com/questions/6839102