开发者

Create PSCredential without a password

How to create a instance of PSCredential that has no password? (Without manually filling out a Get-Credential dialog with no password, this is for unattended running.)

Things I tried:

  1. $mycreds = New-Object System.Management.Automation.PSCredential ("username", $null) Error: Cannot process argument because the valu开发者_如何学编程e of argument "password" is null

  2. $mycreds = New-Object System.Management.Automation.PSCredential ("username", (ConvertTo-SecureString $null -AsPlainText -Force)) Error: ConvertTo-SecureString : Cannot bind argument to parameter 'String' because it is null.

  3. $mycreds = New-Object System.Management.Automation.PSCredential ("username", (ConvertTo-SecureString "" -AsPlainText -Force)) Error: ConvertTo-SecureString : Cannot bind argument to parameter 'String' because it is an empty string.


Solution:

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


Use the Get-Credential cmdlet, when the credential dialog appears, don't type a password

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜