Create Runbooks To Automate Tasks In Azure Automation

Azure Automation

Azure Automation enables the users to automate the tasks, which are manual and repetitive in nature by using Runbooks. Runbooks are nothing but a set of tasks, which performs some automated implementation in an Azure Automation.

Runbooks in Azure Automation are based on Windows PowerShell or Windows PowerShell Workflow. We can code and implement the logic, which we want to automate, using PowerShell.

Azure Automation Accounts

Before getting started with Azure Automation, we have to create Automation Accounts, which will be used to authenticate Runbooks. When we create a new Automation account in Azure portal, it automatically creates a Run As Account and a Classic Run As Account. You can find how to create Azure automation accounts here.

Create Runbook

In this article, we will see how to create a Runbook, which will be used to automate the tasks or implement some logic on behalf of the user.To create a Runbook, go to Azure Automation in Azure portal and click on Runbooks tile.

Azure

Select Add a runbook, so as to create a new one. Before diving into complex Runbooks, we will first create a sample Hello World Runbook , which displays a text ‘hello world’, when it is run.

Azure

Specify Runbook name and the Runbook type. We will see PowerShell Workflow to implement the logic. Click Create to provision Runbook.

Azure

Thus, we have created a sample hello world Runbook.

Azure

Click Edit to modify Runbook and add some logic to it.

Azure

By default, it will be empty. Let’s add the output code given below to the main function.

  1. workflow Hello_World  
  2. {  
  3.    Write-Output "Hello World - Message from First Azure Automation Runbook"  
  4. }  
Azure

When we save it, it will still be in Draft state.

Azure

Before publishing it, we can test it by selecting Test pane option.

Azure

Click Start to test and run Runbook. We can see upper pane shows the run status and lower pane shows the output from the runbook.

Azure

Thus, we have successfully validated Runbook. Now, we can finally Publish it, as shown below.

Azure

It will be listed along with all other Runbooks in the automation account.

Azure

We can start the published Runbook from here.

Azure

Once completed, we can click Output Tile to see the output.

Azure

Thus, we have seen a sample Hello World Runbook in an action. Now, we will see a real world example in the next session.

Azure

Real World Example - Retrieve REST Service Data and Send mail

We will explore a real time example, where Runbook can be used to implement a logic on behalf of the user. We will be using PowerShell to fetch JSON data from a REST Service endpoint and mail the data to the business users. Let’s get started by creating Runbook.

Azure

Specify the name and we have selected Runbook type as PowerShell Workflow. Click Create.

Azure

Let's edit Runbook, which is currently empty.

Azure

We will be adding PowerShell code given below, which uses the ‘Invoke-WebRequest’ command to call the REST Endpoint and get JSON data. Once JSON data has been retrieved, we will use ‘ConvertFrom-Json’ to convert the data from JSON to PowerShell Custom Objects. Afterwards, we will use ConvertTo-HTML to convert the data to HTML format, which will be used as the mail body.

Since we have to mail the data retrieved from REST endpoint to the users, we will be using Office 365 SMTP to relay the mail. Once the parameters are entered, we will use the Send-MailMessage command to E-mail the data retrieved from REST endpoint to the business users. Once the mail has been sent, a custom message indicates successful mail delivery will be shown in the output pane.

  1. workflow FetchRESTData_MailToUsers  
  2. {  
  3.     $request = 'http://services.groupkt.com/state/get/USA/all'  
  4.     $result = Invoke - WebRequest $request - UseBasicParsing  
  5.     $JSONResult = $result | ConvertFrom - Json | select - expand RestResponse | select - expand result  
  6.     $Body = $JSONResult | Select country, name, capital, largest_city | Sort - Object name | ConvertTo - HTML  
  7.     $SmtpServer = 'smtp.office365.com'  
  8.     $SmtpUser = '[email protected]'  
  9.     $smtpPassword = ‘ < Input Office 365 Password Here > ’$MailtTo = '[email protected]'  
  10.     $MailFrom = '[email protected]'  
  11.     $MailSubject = "Test using $SmtpServer"  
  12.     $Credentials = New - Object System.Management.Automation.PSCredential - ArgumentList $SmtpUser, $($smtpPassword | ConvertTo - SecureString - AsPlainText - Force)  
  13.     Send - MailMessage - To "$MailtTo" - from "$MailFrom" - Subject $MailSubject - Body "$Body" - SmtpServer $SmtpServer - BodyAsHtml - UseSsl - Credential $Credentials  
  14.     write - Output "Custom Message : REST Service JSON Data parsed and Email Sent to Business Users"  
  15. }  
Azure

On running the command given above in the Test Pane, we can see the custom message, which is shown after the mail delivery indicating successful run of Runbook. 

Azure

We have also received the data retrieved from the REST endpoint as a mail in the inbox, as shown below.

Azure

Since we have successfully tested the Runbook, let’s publish it.

Azure

This completes the creation of Runbook in Azure. In the upcoming article, we will see how to invoke Azure Runbook, using Webhooks from the client Applications.

Summary

Thus, we saw how to create a Runbook to automate the tasks, using Azure Automation. We also explored a real world example i.e. how to retrieve the data from REST Service, using Azure Automation and mail it to the business users.