Reputation: 47
trying to send an email upon the creation of a new user to the admin. This appears to work the first time but then fails to run a second time. I think its an issue with me creating a new object the second time, but am unfamiliar with PScredential, and how to call it a second time instead of creating it again, I'm assuming it would be some form of if statement, but I don't know what to call in the if.
here is my Code
$password = ConvertTo-SecureString “Password” -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential('38da1ca9daf082',"$password")
Send-MailMessage -SmtpServer 'smtp.mailtrap.io' -Credential $cred -UseSsl -From '[email protected]' -To '[email protected]' -Subject 'TEST'
and this is the error message
New-Object : Cannot find an overload for "PSCredential" and the argument count: "2".
At line:3 char:9
+ $Cred = New-Object System.Management.Automation.PSCredential('38da1ca ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
Upvotes: 0
Views: 67
Reputation: 6860
So lets see the documentation https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.pscredential?view=pscore-6.2.0
We can see there are 2 constructors
PSCredential(PSObject)
or
PSCredential(String, SecureString)
It looks like in the post example PSCredential(String, SecureString)
was trying to be used.
In the example
$password = ConvertTo-SecureString “Password” -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential('38da1ca9daf082',"$password")
Because "$Password"
was put in quotes it has turned the SecureString
into a regular String
.
The Fix is to remove the qoutes "
Here is a working copy
$password = ConvertTo-SecureString “Password” -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential('38da1ca9daf082',$password)
Send-MailMessage -SmtpServer 'smtp.mailtrap.io' -Credential $cred -UseSsl -From '[email protected]' -To '[email protected]' -Subject 'TEST'
Upvotes: 1