New-Object : Cannot find an overload for “PSCredential” and the argument count: “2”

前端 未结 2 681
清歌不尽
清歌不尽 2020-12-30 19:16

I am writing a PowerShell script on a Windows 8.1 machine. When trying to create a PSCredential object using New-Object cmdlet, I was presented with this error:

New-

2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-30 19:59

    In situation like this, you may want to check your parameter type. In this particular example, the input parameter was declared to be a String. However, the result from ConvertTo-SecureString returns a SecureString.

    The error message is a little misleading in this situation. The problem isn't because there is no constructor with 2 arguments but because $userPassword was declared to be a String but later was changed to SecureString.

    [string][ValidateNotNullOrEmpty()] $userPassword = "myPassword",
    
    $userPassword = ConvertTo-SecureString -String $userPassword -AsPlainText -Force
    $userCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "myUserName", $userPassword
    

提交回复
热议问题