Building Your First Azure Automation Runbook
- By Adam Bertram
Knowing how to automate processes in today's IT organizations is critical. System administrators typically have to manage hundreds of servers and a cloud environment and provide support to users.
Luckily, we have tools like Azure Automation to help us out.
If you're not familiar with Azure Automation, I highly encourage you to check out the Azure Automation documentation site. It has lots of great content on helping you understand what it is and what it can do for you.
Today, let's create your first Azure Automation runbook. To do this, I'm assuming that you already have a Microsoft Azure subscription and have an Automation account already created.
To create your first runbook, log into the Azure Portal and navigate to your Automation account, then click on Runbooks. You'll see a lot of prebuilt runbooks available.
Once at this screen, click on the Add Runbook button and on Quick Create (Create a new runbook). Here is where you'll be able to specify a name and type for your runbook.
An Automation runbook can be created to run a few different ways, as shown in the Runbook Type drop-down. At this time, you can choose to write PowerShell or Python code, or create a PowerShell workflow. Running a PowerShell script will likely be the most common use of a runbook, so I'll choose PowerShell as the runbook type.
Once I do that, I'm presented with a page where I can start writing my PowerShell script. I also have various drop-downs on the left side to use inside of my code if I choose to. Since all PowerShell cmdlets come from modules, you'll see all of the modules with commands available on the left. By default, you will have all of the standard modules and all of the Azure modules. You can, however, upload your own modules, as well.
To demonstrate a simple runbook, let's just use a runbook to retrieve a list of Azure VMs in my subscription. I'm just going to run Get-AzureRmVm, click Save and then click on the Publish button to make the runbook live. Once I do that, I can then create a job, which will execute the runbook.
Once I click on Start, the runbook will either start executing immediately or, if you have a Hybrid Runbook worker setup for on-prem execution, it will ask where you'd like to run the PowerShell code. I'll go ahead and run it on Azure. Once I confirm where to run the runbook, the job is queued and waits for a free time to execute.
Once the job is complete, you can view the Job status. If you've been following along, the job would have errored out because I didn't authenticate to my subscription first.
The runbook is aware of the streams that PowerShell has and you will receive output in the appropriate section depending on the stream that was output within your PowerShell code.
I need to authenticate to my Azure subscription, but to do so I need a credential. Because I don't want to store this in plain text in the runbook code itself, I should use a runbook asset. To create a credential asset, I'll go back into my Automation account and create a credential.
Once I have the credential created, I can then go back into my runbook, click on Assets --> Credentials, and you'll see the credential. Right-click and click on Add to Canvas, and it will insert the appropriate PowerShell code to retrieve the credential.
$cred = Get-AutomationPSCredential -Name 'AzureSubscriptionCredential'
Add-AzureRmAccount -Credential $cred
Once saved, published and run again, you'll see that the command was successful. You've created your first runbook!
There's obviously a lot more to Azure Automation runbooks. If you see some potential in this technology, I highly encourage you to dig deeper in the Azure Automation documentation and see what you can come up with.
Adam Bertram is a 20-year veteran of IT. He's an automation engineer, blogger, consultant, freelance writer, Pluralsight course author and content marketing advisor to multiple technology companies. Adam also founded the popular TechSnips e-learning platform. He mainly focuses on DevOps, system management and automation technologies, as well as various cloud platforms mostly in the Microsoft space. He is a Microsoft Cloud and Datacenter Management MVP who absorbs knowledge from the IT field and explains it in an easy-to-understand fashion. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn or follow him on Twitter at @adbertram or the TechSnips Twitter account @techsnips_io.