32

如何创建没有密码的 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”,因为它是一个空字符串。

4

2 回答 2

50

解决方案:

$mycreds = New-Object System.Management.Automation.PSCredential 
              ("username", (new-object System.Security.SecureString))
于 2011-07-27T05:48:42.640 回答
-2

使用 Get-Credential cmdlet,当出现凭据对话框时,不要输入密码

$cred = Get-Credential pebrian27
$cred.GetNetworkCredential() | select UserName,Password

UserName   Password
--------   --------
pebrian27
于 2011-07-27T06:53:18.637 回答