Reputation: 3185
So I have a encrypted string stored in a text file that I can call again and again, this is the password for my SMTP Smart Host. This is created via:
Read-Host "Enter the Password:" -AsSecureString | ConvertFrom-SecureString | Out-File C:\EncPW.bin
I now need to somehow pass this along with a $username
to a -Credential
parameter for a Send-MailMessage
function. I assume I need to get this into a PSCredential format. But I'm stuck !
$password = cat C:\encPW.bin | ConvertTo-SecureString
$password
$cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username,$password
As of now I have this, but it $password when I print it outputs System.Security.SecureString
and I get an error:
net_io_connectionclosed
When it tries to SMTP send.
Cheers
CALL to SMTP Send:
Send-MailMessage -from "[email protected]" -To "[email protected]" -Subject "Daily Report" -Body "Open Attachment for Report" -smtpServer 85.119.248.54 -Attachments "C:\WSB_Reports\DailyReport.txt" -Credential $cred
Upvotes: 0
Views: 3639
Reputation: 202042
The approach you're taking appears correct assuming your username and password are correct. Is your username prefixed with the domain e.g. domain\username
? I second the suggestion to try the command with a manually supplied credential via Get-Credential
e.g.:
$cred = Get-Credential
If you need to verify the password coming back from the secure string is correct, you can display it like so:
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password)
$str = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr)
[System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr)
$str
Upvotes: 2