You can import Azure Automation runbooks using PowerShell very easily. This saves some effort in the Azure Portal if you have a lot of runbooks you wish to import. I’ve written recently about creating an azure automation runbook using PowerShell. The article covers creating the runbook itself, however, it discussed how to edit the runbook code in the Azure Portal. Another approach is to use PowerShell to import the runbook entirely. This short article shows you how!
Import a Azure Automation Runbook with PowerShell
First of all, you will need your runbook code in a .ps1 file, in the location where you are running your Azure PowerShell cmdlets (I tend to use the Azure Cloud Shell). For a simple ‘hello world’ example, I have a PS1 file called runbook.ps1 which has the following content:
write-output "Hello World"
This is a simple example – but this runbook.ps1 file contains the code I want my Azure Automation runbook to execute when it runs. Note that the name of this .ps1 file isn’t significant, so long as it’s specified correctly in the next step.
Next, the runbook can be imported with the following code:
$resoucegroup = "ResourceGroupName"
$automationAccountName = "AutomationAccount"
$runbookName = "My_TestRunbook"
$scriptPath = "runbook.ps1"
Import-AzAutomationRunbook -Name $runbookName -Path $scriptPath `
-ResourceGroupName $resourcegroup -AutomationAccountName $automationAccount `
-Type PowerShell
Note we have to set some variables for this to work, including the Resource Group and Automation Account names, the name we wish to give the runbook and the path to the runbook.ps1 file. Once this runs, you should see it listed in the Automation Account:
Note that when it’s imported the authoring status will be set to New.
Publish a Automation Runbook using PowerShell
We can publish the runbook using PowerShell:
Publish-AzureRmAutomationRunbook -AutomationAccountName $automationAccount -Name $runbookName -ResourceGroupName $resourcegroup
All done! Now you can look to schedule the Azure Automation runbook, now that you have learnt how to import and publish Azure automation runbooks using PowerShell.