In this article, you will learn about the different types of runbooks (yes, there’s more than one of them), how to create a new runbook, and how to add code to that runbook.
In the previous article, Getting Started with Azure Automation: Introduction,you were introduced to Azure Automation, the different types of assets and how to create an Azure Automation Account.
If you haven’t read the previous article in this series yet, I highly recommend you to go back and read it first to catch up with the progress.
Before we go ahead and start creating a runbook, let’s look at the different types of Azure Automation RunBooks.
There are five (5) types of runbooks in Azure Automation and are briefly described below. For further information, click on each Runbook type links.
Graphical PowerShell Workflow: https://docs.microsoft.com/en-us/azure/automation/automation-runbook-types#graphical-runbooks
In this article, only the PowerShell Runbook will be covered.
To create a runbook, the Automation Account must already be created. If not, please go back and read Getting Started with Azure Automation: Introduction which contains the section on how to create an Azure Automation Account.
In the next sections, you’ll learn how to create a PowerShell Runbook using the Portal and PowerShell.
First, log in to the Azure portal and navigate to the automation account that was previously created. (In this article, the automation account is Azure-AA to the automation account ‘Azure-AA.')
Where automation account ‘Azure-AA’ is.
Then from the navigation pane on the left side, click Runbooks. Next, click on Create a runbook.
Runbooks and click ‘Create a runbook’
The Create a runbook form shows up at the right side of the portal.
Fill in the information to create a runbook.
Wait for the runbook creation to complete. Then go back to the Runbooks page to confirm that the new runbook has been created similar to the screenshot below.
Runbook is created.
First, log in to Azure using PowerShell. Then, use this next command to create a new runbook. Just copy and paste the code into your PowerShell session.
The command above creates a new runbook with the name of my-first-runbook using the Azure-AA automation account with the runbook type of PowerShell and inside the Azure-AA-RG resource group.
If the command is successful, you should see a similar output to the screenshot below.
Create a runbook using PowerShell
If you want to retrieve a list of Runbooks, you can use this command below. Replace the ResourceGroupName and the AutomationAccountName with your own.
Get the list of runbooks associated with your subscription.
At this point, a runbook has been created but it does not contain any code yet. Until the runbook is populated with code, it is essentially useless.
Populating the runbook with code can be accomplished using the Portal and using PowerShell. The main difference between the two approaches is the way the script is edited.
When you use the Portal, you can only edit the script using the editor in the Portal itself. While with PowerShell, you can create your scripts offline or using an external editor, import them into the runbook using the Import-AzAutomationRunbook command.
From the Azure Portal, navigate to the runbook. Once inside the runbook overview page, click on Edit
Open the runbook properties.
Next, you will be taken to the runbook editor where you can add your code.
Just for testing, add the code Write-Output 'Hello PowerShell' then click Save. Then the runbook will be saved with the code you just added.
Add test code and save runbook.
NOTE: The file containing the runbook code to be imported must have the extension .ps1. Other file extensions will not be accepted and will result in an error.
Using your editor of choice, create a file named my-first-runbook.ps1. In this example, I am using PowerShell ISE to edit the code and save the file in *C: when I’m done.'
Test script edited in PowerShell ISE.
After saving the script file, import the code to the runbook using this command.
The command above imports the code in C:\Temp\my-first-runbook.ps1 to the PowerShell Runbook named my-first-runbook which belongs to the resource group Azure-AA-RG using the automation account name Azure-AA.
You can tell if the import is successful when you see an output similar to the screenshot shown below.
Test script imported to the existing runbook using PowerShell with the -Force parameter.
The -Force parameter is added because we are forcing an import to an existing runbook. If you do not use the
-Force parameter, the command will result in an error saying that the runbook already exists like the screenshot below.
Importing the script to an existing runbook without the -Force parameter.
PRO TIP: Using the Import-AzAutomationRunbook, when you specify a runbook name that does not exist, it will create a new runbook automatically using the name you provided.
This means that if you have a script ready, you can import the code directly to a new runbook without having to create the runbook separately.
In this article, you learned about the five different types of automation runbooks. You also learned how to create your first PowerShell runbook and add code to runbooks using the Portal and using PowerShell.
In the next article, I will show you how to create and store automation assets like variables and credentials and how to use them in a runbook.
Thank you for reading and stay tuned!
June has been in IT since 2004 and is currently an IT Engineer and Consultant by day. He's also a freelance writer, blogger, and coder. When not dabbling with tech stuff, he's mostly busy with his family, their dogs, and trying hard to be a mechanic.
Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.Learn More
Subscribe to get all the news, info and tutorials you need to build better business apps and sites
You can also ask us not to share your Personal Information to third parties here: Do Not Sell or Share My Info
We see that you have already chosen to receive marketing materials from us. If you wish to change this at any time you may do so by clicking here.
Thank you for your continued interest in Progress. Based on either your previous activity on our websites or our ongoing relationship, we will keep you updated on our products, solutions, services, company news and events. If you decide that you want to be removed from our mailing lists at any time, you can change your contact preferences by clicking here.