Skip to content

Latest commit

 

History

History
121 lines (95 loc) · 4.65 KB

automation-pass-json-string.md

File metadata and controls

121 lines (95 loc) · 4.65 KB
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

Pass a JSON object to an Azure Automation runbook

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.

Prerequisites

To complete this tutorial, you need the following:

Create the JSON file

Type the following test in a text file, and save it as test.json somewhere on your local computer.

{
   "VmName" : "TestVM",
   "ResourceGroup" : "AzureAutomationTest"
}

Create the runbook

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.

Call the runbook from PowerShell

Now you can call the runbook from your local machine by using Azure PowerShell. Run the following PowerShell commands:

  1. Log in to Azure:
    Login-AzureRmAccount
    You are prompted to enter your Azure credentials.
  2. 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.
  3. Convert the string contents of $json to a PowerShell object:
    $JsonParams = @{"json"=$json}
  4. Create a hashtable for the parameters for Start-AzureRmAutomationRunbook:
    $RBParams = @{
         AutomationAccountName = 'AATest'
         ResourceGroupName = 'RGTest'
         Name = 'Test-Json'
         Parameters = $JsonParams
    }
    Notice that you are setting the value of Parameters to the PowerShell object that contains the values from the JSON file.
  5. Start the runbook
    $job = Start-AzureRmAutomationRunbook @RBParams

The runbook uses the values from the JSON file to start a VM.

Next steps