How to Create Your First Azure Automation Runbook with PowerShell

Published:8 October 2024 - 2 min. read

Azure Automation is a powerful service that allows you to automate tasks across your Azure and on-premises environments. At the heart of Azure Automation are runbooks – scripts or workflows that perform specific actions in your Azure environment. In this tutorial, we’ll walk through creating your very first Azure Automation runbook using PowerShell.

Setting the Stage

Before we dive in, let’s set up our environment. We’ll start by creating a resource group to contain all the resources we’ll be using in this tutorial. Fire up PowerShell and run:

New-AzResourceGroup -Name 'AzAutomationTutorial' -Location 'East US'

For convenience, let’s store our resource group name and location in variables:

$resourceGroupName = 'AzAutomationTutorial'
$location = 'East US'

Creating an Automation Account

Next, we need to create an Automation Account. Think of this as a container for all your automation resources, including runbooks, jobs, and assets. Let’s create one:

New-AzAutomationAccount -Name 'MyAzAutomationAccount' -Location $location -ResourceGroupName $resourceGroupName

And again, let’s store the account name for later use:

$automationAccountName = 'MyAzAutomationAccount'

Creating a Runbook

Now comes the fun part – creating our runbook! While we could do this entirely through PowerShell, the command to create a PowerShell 7 runbook is currently a bit buggy. So, we’re going to use the Azure Automation extension in Visual Studio Code.

1. Install the Azure Automation extension in VS Code.

2. Click on the Azure icon in the left sidebar.

3. Sign into your Azure account.

4. Navigate to your Automation Account > Runbooks.

5. Right-click on Runbooks and select “Create runbook”.

6. Name it “MyAzAutomationRunbook” and select PowerShell 7.2 as the type.

Voila! You’ve created your first runbook. Now let’s add some code to it:

Write-Output "Hello from my first Azure Automation runbook!"

Simple, but it’ll do for our first foray into Azure Automation.

Testing and Publishing the Runbook

Before we unleash our runbook on Azure, let’s test it locally. Right-click on the runbook in VS Code and select “Run Local”. If all goes well, you should see our friendly greeting in the output.

Now, let’s publish this masterpiece to Azure. Right-click on the runbook again and choose “Publish Runbook”. Confirm the action, and your runbook is now live in Azure!

We can verify this using PowerShell:

Get-AzAutomationRunbook -ResourceGroupName $resourceGroupName -AutomationAccountName $automationAccountName -Name 'MyAzAutomationRunbook'

If you see your runbook listed with a state of “Published”, you’re golden!

Running the Runbook

Time for the moment of truth – let’s run our runbook in Azure:

Start-AzAutomationRunbook -Name 'MyAzAutomationRunbook' -ResourceGroupName $resourceGroupName -AutomationAccountName $automationAccountName

This command creates a job for our runbook. A job is an instance of a runbook that’s been started – think of it as a way to track the progress of a runbook.

We can check the status of our job:

$job = Get-AzAutomationJob -ResourceGroupName $resourceGroupName -AutomationAccountName $automationAccountName -RunbookName 'MyAzAutomationRunbook'

Once the job is complete, we can retrieve its output:

Get-AzAutomationJobOutput -Id $job.JobId -ResourceGroupName $resourceGroupName -AutomationAccountName $automationAccountName -Stream Output

If all has gone according to plan, you should see our friendly greeting in the Summary property of the output.

Wrapping Up

Congratulations! You’ve just created, published, and run your first Azure Automation runbook using PowerShell. While this example was simple, it demonstrates the basic workflow for working with runbooks in Azure Automation.

From here, you can start building more complex runbooks to automate all sorts of tasks in your Azure environment. Maybe next time we’ll have our runbook do something a bit more exciting than say hello – like starting up a virtual machine army to take over the w… I mean, to improve operational efficiency. Happy automating!

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!