This article will take a look at how to create an Azure Automation runbook. I’ve recently written about how to create an Azure Automation Account using PowerShell, and how to do the same using an ARM template. This article will look at how to use an automation account to run a custom automation runbook.
Azure Automation Runbooks
An Automation Runbook can be used in Azure to Automate just about anything you can code. There are a number of different runbook types. These are:
- Graphical
- Graphical PowerShell Workflow
- PowerShell
- PowerShell Workflow
- Python
You can read about the different types here. In this article I’ll be creating a simple PowerShell runbook.
Create a New PowerShell Runbook
PowerShell can be used to create a new runbook. This initally won’t include the code that we want the runbook to execute, but will create the runbook resource within the automation account:
New-AzureRmAutomationRunbook -Name My-Runbook -Type PowerShell -ResourceGroupName ResourceGroupName -AutomationAccountName AutomationAccountName
The command will only take a second to run. Once done, you will be able to see the new runbook within the automation account:
You can also list all the Azure Runbooks in the automation account using PowerShell:
Get-AzureRmAutomationRunbook -ResourceGroupName ResourceGroupName -AutomationAccountName AutomationAccountName
Adding Code to the Runbook
Next we need to add our code to the runbook. This is the code we want the runbook to execute each time it runs. In keeping with tradition, we will make this first runbook a simple ‘hello world’ script. First of all, in the Azure portal, click the newly created runbook to open it, then click the edit button. Here we will be able to paste in the code we want to run:
Once done, click Save. We can now test the runbook by clicking the Test pane button, then clicking start. After a little while, after the job has been submitted, you will see the output from the runbook:
Publishing and Starting the Runbook
At this point, if you are happy with the output, you can choose to publish the runbook, which will make it available within the automation account. Click the Publish button to publish the runbook and then Yes when prompted. Once published, you can run the runbook by clicking the Start button. This time you will be able to review the output of the script on the output tab:
Schedule the Runbook
Now that it is published, we can use the schedule functionality of the Azure automation account in order to schedule the runbook execution. To do so, whilst in the runbook configuration, select Schedules, then click Add A Schedule. Here you can either link an existing schedule or create a new one:
Once created, click OK. The run book will then be ran automatically on your chosen schedule.
Check the Runbook Logs
You can check the logs for a runbook by clicking the All Logs tab. This will show when the runbook ran, and if any issues were encountered:
The log shows the time the runbook ran, and its output.
Final words
In this article you have learnt how to create a simple Azure Automation runbook along with how to publish it, schedule it and how to check the logs for any issues.