.net - New-Object : Cannot find an overload for "PSCredential" and the argument count: "2" -
i writing powershell script on windows 8.1 machine. when trying create pscredential object using new-object cmdlet, presented error:
new-object : cannot find overload "pscredential" , argument count: "2".
running exact same script on windows 8.1 machine works fine me. have verified both machines running same version of powershell 4.0
both machines have same .net framework installed 4.0.
any idea why happening , how resolve issue?
$userpassword = convertto-securestring -string "mypassword" -asplaintext -force $usercredential = new-object -typename system.management.automation.pscredential -argumentlist "myusername", $userpassword
after more testing, found out problem. function, intended take username , password user provide default values if user decide skip input parameters.
for achieved adding following line in parameters section
[string][validatenotnullorempty()] $userpassword = "mypassword",
it seems problem defined [string] in parameter later trying securestring, resulted in problem.
removing [string] attribute in parameter solved problem.
in situation this, may want check parameter type. in particular example, input parameter declared string. however, result convertto-securestring returns securestring.
the error message little misleading in situation. problem isn't because there no constructor 2 arguments because $userpassword declared string later changed securestring.
[string][validatenotnullorempty()] $userpassword = "mypassword", $userpassword = convertto-securestring -string "mypassword" -asplaintext -force $usercredential = new-object -typename system.management.automation.pscredential -argumentlist "myusername", $userpassword
Comments
Post a Comment