We want secure Passwords to use in our scripts rather than leaving them in plain text. So this is how we do it.
We run this in the working directory of your script to setup the “SecurePassword.txt” File. It will prompt you for your password.
Read-Host “Input Password” -AsSecureString | ConvertFrom-SecureString | Out-File .\SecurePassword.txt
To put your mind at ease you can run
Get-Content .\SecurePassword.Txt
to see how your password is being stored.
2. Put into your Script
Edit the Username for your account. And TA DA! Done like a turkey.
$UserName = “[email protected]”
$Password = Get-Content .\SecurePassword.txt | ConvertTo-SecureString
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName, $Password
As long as you keep the text file in the same directory
This is the template i use for making credential files. I hope it helps
<#
.SYNOPSIS
Setting up and using a credential template.DESCRIPTION
.EXAMPLE
.NOTES
Author. Luke Keam
Published. on 20/11/2016 AUS
Version. 1
Company Name. techgeek.biz
Copyright. Free for personal and commercial use. As long as reference kept.Questions, comments, suggestions always welcome. Please email me.
.LINK
Website. /techtips/powershell-storing-secure-passwords-with-pscredential/
Website. techgeek.biz
Email. [email protected]
#># Set a password by running this
Read-Host “Input Password” -AsSecureString | ConvertFrom-SecureString | Out-File .\SecurePassword.txt
# Then leave this template in script that you are using
$UserName = “[email protected]”
$Password = Get-Content .\SecurePassword.txt | ConvertTo-SecureString
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName, $Password
Send-MailMessage -Credential $Credential
Hope this was helpful
Have fun