This page looks best with JavaScript enabled

E-mail Anywhere In Your PowerShell Scripts

 ·  ☕ 4 min read

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-mail.outlook.com

Here’s the break down of the PowerShell script with an explanation of the code:

Here we configure the command line switches available.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
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.

1
2
3
4
5
If ($logpath) {
    $logfile = "test.log"
    $log = "$logpath\$logfile"
    Start-Transcript $log
}

Because this is just an example script, here are some example commands.

1
2
3
Write-Host "Hello World 1"
Write-Host "Hello World 2"
Write-Host "Hello World 3"

If logging was configured, here the log is stopped.

1
2
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.

1
2
3
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.

1
2
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.

1
2
3
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.

1
2
3
4
5
        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.

1
2
3
4
5
        Else {
            Send-MailMessage -To $mailto -From $mailfrom -Subject $mailsubject -Body $mailbody -SmtpServer $smtpserver
         }
    }
}

Here’s the complete PowerShell script:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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 have any questions or comments, please leave them below.

-Mike

Share on
Support the author with