Invoke Azure Automation jobs using PowerShell

We can use PowerShell cmdlet Get-AzureRmAutomationRunbook to get runbooks associated with an automation account:

$resourceGroupName = "enggdevsoutheastasia"
$automationAccountName = "AzureAutomation"
$runbookName = "start-azurevms"
Get-AzureRmAutomationRunbook -automationAccountName $automationAccountName `
 -resourceGroupName $resourceGroupName

Similarly, we can use cmdlet Start-AzureRmAutomationRunbook to start runbook:

$resourceGroupName = "enggdevsoutheastasia"
$automationAccountName = "AzureAutomation"
$runbookName = "start-azurevms"
$job = Start-AzureRmAutomationRunbook –AutomationAccountName $automationAccountName `
 -Name $runbookName `
 -ResourceGroupName $resourceGroupName

Start-AzureRmAutomationRunbook returns a job object that you can use to track its status once the runbook is started. We can then use this job object with Get-AzureRmAutomationJob to determine the status of the job and Get-AzureRmAutomationJobOutput to get its output. The following sample code starts a runbook called Test-Runbook, waits until it has completed, and then displays its output:

$resourceGroupName = "enggdevsoutheastasia"
$automationAccountName = "AzureAutomation"
$runbookName = "start-azurevms"
$job = Start-AzureRmAutomationRunbook –AutomationAccountName $automationAccountName `
 -Name $runbookName `
 -ResourceGroupName $resourceGroupName

doLoop = $true
While ($doLoop) {
 $job = Get-AzureRmAutomationJob –AutomationAccountName $automationAccountName -Id $job.JobId `
 -ResourceGroupName $resourceGroupName
 $status = $job.Status
 $doLoop = (($status -ne "Completed") -and ($status -ne "Failed") -and ($status -ne "Suspended") -and ($status -ne "Stopped"))
}

Get-AzureRmAutomationJobOutput –AutomationAccountName $automationAccountName `
 -Id $job.JobId `
 -ResourceGroupName $resourceGroupName `
 –Stream Output

If the runbook requires parameters, then we must provide them as a hashtable where the key of the hashtable matches the parameter name and the value is the parameter value.

The following example shows how to start a runbook with one string parameter named ResourceGroupName.

$params = @{"ResourceGroupName"="enggdevsoutheastasia"}
Start-AzureRmAutomationRunbook –AutomationAccountName $automationAccountName `
 -Name $runbookName `
 -ResourceGroupName $resourceGroupName `
 –Parameters $params

When we start a runbook from the Azure Portal or Windows PowerShell, the instruction is sent through the Azure Automation web service. This service does not support parameters with complex data types. However special formats of object such as arrays or PSCustomObjects are supported using JSON.

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 
   ) 
 
   foreach($i in $user){
        $i
   }
}

The following text could be used for the user parameter: [1,2,3]

If you need more complex datatypes, you would need to use child runbooks for same and call it inline. Guess that’s topic for another post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s