Start a runbook in Azure Automation
The following table helps you determine the method to start a runbook in Azure Automation that is most suitable to your particular scenario. This article includes details on starting a runbook with the Azure portal and with Windows PowerShell. Details on the other methods are provided in other documentation that you can access from the links below.
Method | Characteristics |
---|---|
Azure portal | |
Windows PowerShell | |
Azure Automation API | |
Webhooks | |
Respond to Azure Alert | |
Schedule | |
From Another Runbook |
The following image illustrates detailed step-by-step process in the life cycle of a runbook. It includes different ways a runbook starts in Azure Automation, which components required for Hybrid Runbook Worker to execute Azure Automation runbooks, and interactions between different components. To learn about executing Automation runbooks in your datacenter, refer to hybrid runbook workers
Work with runbook parameters
When you start a runbook from the Azure portal or Windows PowerShell, the instruction is sent through the Azure Automation web service. This service doesn't support parameters with complex data types. If you need to provide a value for a complex parameter, then you must call it inline from another runbook as described in Child runbooks in Azure Automation.
The Azure Automation web service provides special functionality for parameters using certain data types as described in the following sections.
Named values
If the parameter is data type [object], then you can use the following JSON format to send it a list of named values: {Name1:'Value1', Name2:'Value2', Name3:'Value3'}. These values must be simple types. The runbook receives the parameter as a PSCustomObject with properties that correspond to each named value.
Consider the following test runbook that accepts a parameter called user.
Workflow Test-Parameters
{
param (
[Parameter(Mandatory=$true)][object]$user
)
$userObject = $user | ConvertFrom-JSON
if ($userObject.Show) {
foreach ($i in 1..$userObject.RepeatCount) {
$userObject.FirstName
$userObject.LastName
}
}
}
The following text could be used for the user parameter.
{FirstName:'Joe',LastName:'Smith',RepeatCount:'2',Show:'True'}
This results in the following output:
Joe
Smith
Joe
Smith
Arrays
If the parameter is an array such as [array] or [string[]], then you can use the following JSON format to send it a list of values: [Value1, Value2, Value3]. These values must be simple types.
Consider the following test runbook that accepts a parameter called user.
Workflow Test-Parameters
{
param (
[Parameter(Mandatory=$true)][array]$user
)
if ($user[3]) {
foreach ($i in 1..$user[2]) {
$ user[0]
$ user[1]
}
}
}
The following text could be used for the user parameter.
["Joe","Smith",2,true]
This results in the following output:
Joe
Smith
Joe
Smith
Credentials
If the parameter is data type PSCredential
, you can provide the name of an Azure Automation credential asset. The runbook retrieves the credential with the name that you specify. The following test runbook accepts a parameter called credential
.
Workflow Test-Parameters
{
param (
[Parameter(Mandatory=$true)][PSCredential]$credential
)
$credential.UserName
}
The following text could be used for the user parameter assuming that there was a credential asset called My Credential
.
My Credential
Assuming that the user name in the credential is jsmith
, the following output is displayed.
jsmith
Start a runbook with the Azure portal
- In the Azure portal, select Automation and then select the name of an Automation account.
- From the left-hand pane, select Runbooks.
- On the Runbooks page, select a runbook, and then click Start.
- If the runbook has parameters, you're prompted to provide values with a text box for each parameter. For more information on parameters, see Runbook Parameters.
- On the Job pane, you can view the status of the runbook job.
Start a runbook with PowerShell
You can use the Start-AzAutomationRunbook to start a runbook with Windows PowerShell. The following sample code starts a runbook called Test-Runbook.
Start-AzAutomationRunbook -AutomationAccountName "MyAutomationAccount" -Name "Test-Runbook" -ResourceGroupName "ResourceGroup01"
Start-AzAutomationRunbook
returns a job object that you can use to track status once the runbook is started. You can then use this job object with Get-AzAutomationJob to determine the status of the job and Get-AzAutomationJobOutput to retrieve its output. The following example starts a runbook called Test-Runbook, waits until it has completed, and then displays its output.
$runbookName = "Test-Runbook"
$ResourceGroup = "ResourceGroup01"
$AutomationAcct = "MyAutomationAccount"
$job = Start-AzAutomationRunbook -AutomationAccountName $AutomationAcct -Name $runbookName -ResourceGroupName $ResourceGroup
$doLoop = $true
While ($doLoop) {
$job = Get-AzAutomationJob -AutomationAccountName $AutomationAcct -Id $job.JobId -ResourceGroupName $ResourceGroup
$status = $job.Status
$doLoop = (($status -ne "Completed") -and ($status -ne "Failed") -and ($status -ne "Suspended") -and ($status -ne "Stopped"))
}
Get-AzAutomationJobOutput -AutomationAccountName $AutomationAcct -Id $job.JobId -ResourceGroupName $ResourceGroup -Stream Output
If the runbook requires parameters, then you must provide them as a hashtable. The key of the hashtable must match the parameter name and the value is the parameter value. The following example shows how to start a runbook with two string parameters named FirstName and LastName, an integer named RepeatCount, and a boolean parameter named Show. For more information on parameters, see Runbook Parameters.
$params = @{"FirstName"="Joe";"LastName"="Smith";"RepeatCount"=2;"Show"=$true}
Start-AzAutomationRunbook -AutomationAccountName "MyAutomationAccount" -Name "Test-Runbook" -ResourceGroupName "ResourceGroup01" -Parameters $params
Next steps
- For details of runbook management, see Manage runbooks in Azure Automation.
- For PowerShell details, see PowerShell Docs.
- To troubleshoot issues with runbook execution, see Troubleshoot runbook issues.