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

.NetPowershell

.Net Problem Overview


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-Object : Cannot find an overload for "PSCredential" and the argument count: "2".

Running the exact same script on another Windows 8.1 machine works fine for me. I have also verified that both machines are running the same version of PowerShell 4.0

Both machines have the same .NET Framework installed 4.0.

Any idea why this is happening and how I could resolve this issue?

$userPassword = ConvertTo-SecureString -String "MyPassword" -AsPlainText -Force
$userCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "myUserName", $userPassword

After some more testing, I found out the problem. For my function, I intended to take the username and password from the user but also provide default values if the user decide to skip those input parameters.

For that I achieved it by adding the following line in parameters section

[string][ValidateNotNullOrEmpty()] $userPassword = "myPassword",

It seems the problem is that I defined it to be a [String] in the parameter but later trying to be a SecureString, which resulted in the problem.

Removing the [String] attribute in my parameter solved the problem.

.Net Solutions


Solution 1 - .Net

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

Solution 2 - .Net

This situation also happens if you quote your secure string object:

Executing this

$secPassword = ConvertTo-SecureString "mypassword" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ("myusername", "$secPassword")

will return the mentioned error.

Whereas

$secPassword = ConvertTo-SecureString "mypassword" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ("myusername", $secPassword)

will succeed.

Solution 3 - .Net

Also remember that if you don't need a specific credential other than the current credential of the person running the command (or script), you can skip all this with a one-liner:

$Cred = Get-Credential

A popup will then prompt you to enter your username and password. Just like that, you've captured your credentials in a variable that you can use at the command line or you can use this in a script to prompt the user for their credentials.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJacksonView Question on Stackoverflow
Solution 1 - .NetJacksonView Answer on Stackoverflow
Solution 2 - .NetadamencyView Answer on Stackoverflow
Solution 3 - .NetJason EnochsView Answer on Stackoverflow