新对象:找不到“ PSCredential”的重载,并且参数计数为“ 2” [英] New-Object : Cannot find an overload for “PSCredential” and the argument count: “2”

查看:595
本文介绍了新对象:找不到“ PSCredential”的重载,并且参数计数为“ 2”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将一封电子邮件发送给多个收件人,而且我也不想输入用户名和密码。因此,我使用了下面的字符串转换,但是随后我遇到了下面的错误消息。

I want an email to be sent to more than one recipients, and also I don't want to prompt for the username and password. So I have used the below string conversion, but then I'm facing the below error message.

请问您提出的答案可以解决此问题?

Could you please suggest your answers to rectify this issue?

[string] [ValidateNotNullOrEmpty()] $secpasswd = "Q$$777LV"
$secpasswd = ConvertTo-SecureString -String "Q$$777LV" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ("test", $secpasswd)

错误消息:


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

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


推荐答案

您的第一个语句使 $ secpasswd 类型为 [string] 的变量。因此,使用第二条语句创建的 SecureString 对象将自动转换为字符串。因此,这两个语句

Your first statement makes $secpasswd a variable of the type [string]. Because of that the SecureString object that you create with your second statement is automatically converted to a string. Because of this the two statements

[string]$secpasswd = "something"
$secpasswd = ConvertTo-SecureString -String "something" -AsPlainText -Force

实际上与

$secpasswd = 'System.Security.SecureString'

由于 PSCredential 构造函数需要一个字符串和一个 SecureString 对象,而不是两个字符串,因此它将抛出您观察到的错误。

Since the PSCredential constructor expects a string and a SecureString object, not two strings, it throws the error you observed.

要解决此问题,请不要将变量强制为 [string] 类型:

To fix the issue either don't force the variable to the type [string]:

[ValidateNotNullOrEmpty()]$secpasswd = "something"
$secpasswd = ConvertTo-SecureString -String $secpasswd -AsPlainText -Force
$mycreds = New-Object Management.Automation.PSCredential ("test", $secpasswd)

或对明文和安全字符串密码使用不同的变量:

or use different variables for plaintext and secure string password:

[string][ValidateNotNullOrEmpty()]$passwd = "something"
$secpasswd = ConvertTo-SecureString -String $passwd -AsPlainText -Force
$mycreds = New-Object Management.Automation.PSCredential ("test", $secpasswd)

这篇关于新对象:找不到“ PSCredential”的重载,并且参数计数为“ 2”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆