Script Sample - Azure Automation - Sending Email Notification

@20aman    Sep 22, 2018

Sending email notifications from Azure Automation is a requirement that you will encounter a lot when working with any automation scenario. You may want to send an email notification on the completion of the job or on the failure of the job.

This script sample helps you bootstrap the Runbook for you.

Script Inputs and Requirements

The script takes the following input parameters:

  1. SMTP Server URL
  2. SMTP Server Port
  3. User Credentials - Boolean value to check if user credentials are required or not
  4. Credential Name - Credential name for the credential asset in your Azure Automation Account
  5. EnableSsl - Boolean value to select if you want to enable SSL or not
  6. Email From - From address for the email
  7. Email To - Receipient of the email
  8. Email Subject - Subject of the email 9 Message Body - This can be html. So you can compose your email to make it look more professional

The script also requires you to have a Credential asset in your Azure Automation Account. It's name is specified in the 4rth parameter above. This credential is used to connect to the SMTP server. In the sample this smtp server is an O365 email account.

Script Working

The script uses a SMTP Client to send the email. This client is created using the below cmdlet.

$SmtpClient = New-Object System.Net.Mail.SmtpClient $SMTPServerUrl, $SMTPServerPort

where $SMTPServerUrl is the URL of the SMTP server and $SMTPServerPort is the port number used by the SMTP server.

Later, this client is used to send the email by using it's send function as below.


Location of the Script

You can find this script in GitHub at this location: Send-EmailNotificationFromAzureAutomationRunbook.ps1

Comments powered by Disqus