In my previous scripts I’ve included a function to send log files to an on-premises Exchange server. I’ve now put together an example script to improve this function to send emails to external e-mail providers, for example: Office 365, Outlook.com or GMail.com.
This new code is designed to support many different requirements:
- No logging at all.
- Logging but with no e-mail.
- Logging with e-mail.
- E-mail with username & password authentication, with SSL.
- E-mail with username & password authentication, without SSL.
- E-mail with no authentication at all.
Please note: If you have two-factor authentication setup for the external email provider you’ll need to generate an application specific password to use.
- SMTP server for Office 365: smtp.o365.com
- SMTP server for Outlook.com: smtp.live.com
Here’s the break down of the PowerShell script with an explanation of the code:
Here we configure the command line switches available.
Param( [alias("l")] $logpath, [alias("sendto")] $mailto, [alias("from")] $mailfrom, [alias("smtp")] $smtpserver, [alias("user")] $smtpuser, [alias("pwd")] $smtppwd, [switch]$usessl)
Now, if the logging option was configured, the script sets the log output file location and starts logging.
If ($logpath) { $logfile = "test.log" $log = "$logpath\$logfile" Start-Transcript $log }
Because this is just an example script, here are some example commands.
Write-Host "Hello World 1" Write-Host "Hello World 2" Write-Host "Hello World 3"
If logging was configured, here the log is stopped.
If ($logpath) { Stop-Transcript
Now comes the complicated part. If logging was configured then the script also checks if an SMTP server was configured and if it was then the variables for the e-mail subject line and the body of the e-mail are created.
If ($smtpserver) { $mailsubject = "Test Log" $mailbody = Get-Content -Path $log | Out-String
If an e-mail password was configured then a variable containing the username and password is created.
If ($smtppwd) { $smtpcreds = New-Object System.Management.Automation.PSCredential -ArgumentList $smtpuser, $($smtppwd | ConvertTo-SecureString -AsPlainText -Force)
If SSL was configured, then the script uses the variables created previously and executes the Send-MailMessage command using SSL.
If ($usessl) { Send-MailMessage -To $mailto -From $mailfrom -Subject $mailsubject -Body $mailbody -SmtpServer $smtpserver -UseSsl -Credential $smtpcreds }
If SSL was not configured then the script sends the e-mail without using the SSL option.
Else { Send-MailMessage -To $mailto -From $mailfrom -Subject $mailsubject -Body $mailbody -SmtpServer $smtpserver -Credential $smtpcreds } } }
Here we go all the way back to if a user name and password were configured, and if they weren’t then the script will attempt to send the email without any authentication.
Else { Send-MailMessage -To $mailto -From $mailfrom -Subject $mailsubject -Body $mailbody -SmtpServer $smtpserver } } }
Here’s the complete PowerShell script:
Param( [alias("l")] $logpath, [alias("sendto")] $mailto, [alias("from")] $mailfrom, [alias("smtp")] $smtpserver, [alias("user")] $smtpuser, [alias("pwd")] $smtppwd, [switch]$usessl) ##Start log If ($logpath) { $logfile = "test.log" $log = "$logpath\$logfile" Start-Transcript $log } Write-Host "Hello World 1" Write-Host "Hello World 2" Write-Host "Hello World 3" If ($logpath) { Stop-Transcript ## If email was configured, set the variables for the email subject and body If ($smtpserver) { $mailsubject = "Image Factory Log" $mailbody = Get-Content -Path $log | Out-String ## If an email password was configured, create a variable with the username and password If ($smtppwd) { $smtpcreds = New-Object System.Management.Automation.PSCredential -ArgumentList $smtpuser, $($smtppwd | ConvertTo-SecureString -AsPlainText -Force) ## If ssl was configured, send the email with ssl If ($usessl) { Send-MailMessage -To $mailto -From $mailfrom -Subject $mailsubject -Body $mailbody -SmtpServer $smtpserver -UseSsl -Credential $smtpcreds } ## If ssl wasn't configured, send the email without ssl Else { Send-MailMessage -To $mailto -From $mailfrom -Subject $mailsubject -Body $mailbody -SmtpServer $smtpserver -Credential $smtpcreds } } ## If an email username and password were not configured, send the email without authentication Else { Send-MailMessage -To $mailto -From $mailfrom -Subject $mailsubject -Body $mailbody -SmtpServer $smtpserver } } } ## End
If you’d like to get in touch with me please use the comments, Twitter or my contact form.
I hope this article helps you out, please consider supporting my work here. Thank you.
-Mike
Leave a Reply