title | description | services | documentationcenter | author | manager | keywords | ms.service | ms.devlang | ms.topic | ms.tgt_pltfrm | ms.workload | ms.date | ms.author |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Pass a JSON object to an Azure Automation runbook | Microsoft Docs |
How to pass parameters to a runbook as a JSON object |
automation |
dev-center-name |
eslesar |
carmonm |
powershell, runbook, json, azure automation |
automation |
NA |
article |
powershell |
TBD |
06/15/2017 |
eslesar |
It can be useful to store data that you want to pass to a runbook in a JSON file. For example, you might create a JSON file that contains all of the parameters you want to pass to a runbook. To do this, you have to convert the JSON to a string and then convert the string to a PowerShell object before passing its contents to the runbook.
In this example, we'll create a PowerShell script that calls Start-AzureRmAutomationRunbook to start a PowerShell runbook, passing the contents of the JSON to the runbook. The PowerShell runbook starts an Azure VM, getting the parameters for the VM from the JSON that was passed in.
To complete this tutorial, you need the following:
- Azure subscription. If you don't have one yet, you can activate your MSDN subscriber benefits or sign up for a free account.
- Automation account to hold the runbook and authenticate to Azure resources. This account must have permission to start and stop the virtual machine.
- An Azure virtual machine. We stop and start this machine so it should not be a production VM.
- Azure Powershell installed on a local machine. See Install and configure Azure Powershell for information about how to get Azure PowerShell.
Type the following test in a text file, and save it as test.json
somewhere on your local computer.
{
"VmName" : "TestVM",
"ResourceGroup" : "AzureAutomationTest"
}
Create a new PowerShell runbook named "Test-Json" in Azure Automation. To learn how to create a new PowerShell runbook, see My first PowerShell runbook.
To accept the JSON data, the runbook must take an object as an input parameter.
The runbook can then use the properties defined in the JSON.
Param(
[parameter(Mandatory=$true)]
[object]$json
)
# Connect to Azure account
$Conn = Get-AutomationConnection -Name AzureRunAsConnection
Add-AzureRMAccount -ServicePrincipal -Tenant $Conn.TenantID `
-ApplicationID $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint
# Convert object to actual JSON
$json = $json | ConvertFrom-Json
# Use the values from the JSON object as the parameters for your command
Start-AzureRmVM -Name $json.VMName -ResourceGroupName $json.ResourceGroup
Save and publish this runbook in your Automation account.
Now you can call the runbook from your local machine by using Azure PowerShell. Run the following PowerShell commands:
- Log in to Azure:
You are prompted to enter your Azure credentials.
Login-AzureRmAccount
- Get the contents of the JSON file and convert it to a string:
$json = (Get-content -path 'JsonPath\test.json' -Raw) | Out-string
JsonPath
is the path where you saved the JSON file. - Convert the string contents of
$json
to a PowerShell object:$JsonParams = @{"json"=$json}
- Create a hashtable for the parameters for
Start-AzureRmAutomationRunbook
:Notice that you are setting the value of$RBParams = @{ AutomationAccountName = 'AATest' ResourceGroupName = 'RGTest' Name = 'Test-Json' Parameters = $JsonParams }
Parameters
to the PowerShell object that contains the values from the JSON file. - Start the runbook
$job = Start-AzureRmAutomationRunbook @RBParams
The runbook uses the values from the JSON file to start a VM.
- To learn more about editing PowerShell and PowerShell Workflow runbooks with a textual editor, see Editing textual runbooks in Azure Automation
- To learn more about creating and importing runbooks, see Creating or importing a runbook in Azure Automation