Powershell:使用New-Object cmdlet创建PSCredential对象时出错

时间:2018-06-13 17:36:09

标签: powershell powershell-v4.0 powershell-remoting cmdlets

我正在使用PS 5.0版

尝试使用New-Object cmdlet创建PSCredential对象时,我收到此错误:

New-Object : Cannot find an overload for "PSCredential" and the argument count: "2".

我的PS代码如下:

[string][ValidateNotNullOrEmpty()] $secureStringPwd = "pass"

$secureStringPwd = $secureStringPwd|ConvertTo-SecureString -AsPlainText -Force

$creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "svcacct", $secureStringPwd

正如您所看到的,我正在将密码转换为安全字符串,但仍然出现错误

1 个答案:

答案 0 :(得分:1)

以这种方式试试

[ValidateNotNullOrEmpty()] $secureStringPwd = "pass"

$secureStringPwd = $secureStringPwd|ConvertTo-SecureString -AsPlainText -Force

($creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "svcacct", $secureStringPwd)

# Results

UserName                     Password
--------                     --------
svcacct  System.Security.SecureString

或者这样

[string][ValidateNotNullOrEmpty()]$secureStringPwd = "pass"
$secpasswd = ConvertTo-SecureString $secureStringPwd -AsPlainText -Force
($creds = New-Object System.Management.Automation.PSCredential ("username", $secpasswd))


# Results
UserName                     Password
--------                     --------
username System.Security.SecureString