Script Sample - Azure Automation - Sending Email Notification
@20aman Sep 22, 2018Sending 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:
- SMTP Server URL
- SMTP Server Port
- User Credentials - Boolean value to check if user credentials are required or not
- Credential Name - Credential name for the credential asset in your Azure Automation Account
- EnableSsl - Boolean value to select if you want to enable SSL or not
- Email From - From address for the email
- Email To - Receipient of the email
- 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.
$SmtpClient.Send($Message)
Location of the Script
You can find this script in GitHub at this location: Send-EmailNotificationFromAzureAutomationRunbook.ps1