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.
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.
Specify Runbook name and the Runbook type. We will see PowerShell Workflow to implement the logic. Click Create to provision Runbook.
Thus, we have created a sample hello world Runbook.
Click Edit to modify Runbook and add some logic to it.
By default, it will be empty. Let’s add the output code given below to the main function.
- workflow Hello_World
- {
- Write-Output "Hello World - Message from First Azure Automation Runbook"
- }
When we save it, it will still be in Draft state.
Before publishing it, we can test it by selecting Test pane option.
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.
Thus, we have successfully validated Runbook. Now, we can finally Publish it, as shown below.
It will be listed along with all other Runbooks in the automation account.
We can start the published Runbook from here.
Once completed, we can click Output Tile to see the output.
Thus, we have seen a sample Hello World Runbook in an action. Now, we will see a real world example in the next session.
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.
Specify the name and we have selected Runbook type as PowerShell Workflow. Click Create.
Let's edit Runbook, which is currently empty.
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.
- workflow FetchRESTData_MailToUsers
- {
- $request = 'http://services.groupkt.com/state/get/USA/all'
- $result = Invoke - WebRequest $request - UseBasicParsing
- $JSONResult = $result | ConvertFrom - Json | select - expand RestResponse | select - expand result
- $Body = $JSONResult | Select country, name, capital, largest_city | Sort - Object name | ConvertTo - HTML
- $SmtpServer = 'smtp.office365.com'
- $SmtpUser = '[email protected]'
- $smtpPassword = ‘ < Input Office 365 Password Here > ’$MailtTo = '[email protected]'
- $MailFrom = '[email protected]'
- $MailSubject = "Test using $SmtpServer"
- $Credentials = New - Object System.Management.Automation.PSCredential - ArgumentList $SmtpUser, $($smtpPassword | ConvertTo - SecureString - AsPlainText - Force)
- Send - MailMessage - To "$MailtTo" - from "$MailFrom" - Subject $MailSubject - Body "$Body" - SmtpServer $SmtpServer - BodyAsHtml - UseSsl - Credential $Credentials
- write - Output "Custom Message : REST Service JSON Data parsed and Email Sent to Business Users"
- }
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.
We have also received the data retrieved from the REST endpoint as a mail in the inbox, as shown below.
Since we have successfully tested the Runbook, let’s publish it.
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.