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

Powershell: Error while creating a PSCredential object using New-Object cmdlet

我正在使用 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

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

这样试试

[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