无需密码即可创建PSCredential

时间:2011-07-27 03:19:28

标签: .net powershell powershell-v2.0 system.management

如何在PSCredential中创建没有密码的实例?

我尝试的事情:

  1. $mycreds = New-Object System.Management.Automation.PSCredential ("username", $null) 错误:无法处理参数,因为参数“password”的值为null

  2. $mycreds = New-Object System.Management.Automation.PSCredential ("username", (ConvertTo-SecureString $null -AsPlainText -Force)) 错误:ConvertTo-SecureString:无法将参数绑定到参数'String',因为它为null。

  3. $mycreds = New-Object System.Management.Automation.PSCredential ("username", (ConvertTo-SecureString "" -AsPlainText -Force)) 错误:ConvertTo-SecureString:无法将参数绑定到参数'String',因为它是一个空字符串。

2 个答案:

答案 0 :(得分:31)

解决方案:

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

答案 1 :(得分:-2)

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

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

UserName   Password
--------   --------
pebrian27