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)
Error message:
New-Object : Cannot find an overload for “PSCredential” and the argument count: “2”
This error will not rise if data type is defined for secure string.
Your first statement makes
$secpasswd
a variable of the type[string]
. Because of that theSecureString
object that you create with your second statement is automatically converted to a string. Because of this the two statementsare effectively the same as
Since the
PSCredential
constructor expects a string and aSecureString
object, not two strings, it throws the error you observed.To fix the issue either don't force the variable to the type
[string]
:or use different variables for plaintext and secure string password: