.net 新对象:找不到“PSCredential”的重载和参数计数:“2”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22970505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:42:40  来源:igfitidea点击:

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

.netpowershell

提问by xenoverse

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:

我正在 Windows 8.1 机器上编写 PowerShell 脚本。尝试使用 New-Object cmdlet 创建 PSCredential 对象时,出现以下错误:

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

新对象:找不到“PSCredential”和参数计数的重载:“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

在另一台 Windows 8.1 机器上运行完全相同的脚本对我来说效果很好。我还验证了两台机器都运行相同版本的 PowerShell 4.0

Both machines have the same .NET Framework installed 4.0.

两台机器都安装了相同的 .NET Framework 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.

似乎问题是我在参数中将其定义为 [String] 但后来尝试成为 SecureString,从而导致了问题。

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

删除参数中的 [String] 属性解决了问题。

回答by xenoverse

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.

在这种情况下,您可能需要检查您的参数类型。在这个特定的例子中,输入参数被声明为一个字符串。但是, ConvertTo-SecureString 的结果返回 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 $userPasswordwas declared to be a Stringbut later was changed to SecureString.

在这种情况下,错误消息有点误导。问题不是因为没有带有 2 个参数的构造函数,而是因为$userPassword被声明为 aString但后来改为SecureString.

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

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

回答by Jason Enochs

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.

然后会弹出一个窗口,提示您输入用户名和密码。就像那样,您已经在一个变量中捕获了您的凭据,您可以在命令行中使用该变量,或者您可以在脚本中使用它来提示用户输入凭据。