Passing Complex Parameters to New-AzureRmResourceGroupDeployment inside an Azure Automation Runbook

An Azure Automation Runbook can be triggered by a webhook and this allows external events to initiate actions inside your environment through the use of an ARM template and New-AzureRmResourceGroupDeployment. Of course, to make the runbook as flexible and useful as possible, Azure Automation Variable Assets should be used to pass in ARM template parameters via the TemplateParameterObject parameter of New-AzureRmResourceGroupDeployment. For the most part, this is straightforward – all variable assets are strings and inserting a string into a hashtable (the data type of TemplateParameterObject) is simple. But what about complex objects? Sometimes you want to pass a JSON object as a parameter. 

In the simplest case, we pass a single string variable:

 
[sourcecode language='powershell' ]
$resourceGroupString = Get-AutomationVariable -Name ‘my_rg_name’
$locationString      = Get-AutomationVariable -Name ‘my_location’
$templateUri         = Get-AutomationVariable -Name ‘my_template_uri’

$parameters = @{}
$parameters.Add(“location”, $locationString)

New-AzureRmResourceGroupDeployment `
  -Mode Incremental `
  -Name myTestDeployment `
  -ResourceGroupName $resourceGroupString `
  -TemplateUri $templateUri `
  -TemplateParameterObject $parameters

 

Now let’s add in a complex object. Let’s say we want to pass in the imageReference object exactly as it’ll be used by the Microsoft.Compute/virtualMachines resource. Here’s a sample of the JSON:

 
[sourcecode language='powershell' ]
“imageReference”: {
  “publisher”: “MicrosoftWindowsServer”,
  “offer”: ”WindowsServer”,
  “sku”: ”2012-R2-Datacenter”,
  “version”: ”latest”
}

This object can be pasted directly into an Azure Automation variable asset via the portal UI (minus the formatting). Bringing it into the runbook is straightforward:

 
[sourcecode language='powershell' ]
$imageReferenceString = Get-AutomationVariable -Name ‘my_imageReference’

But making it suitable for passing into TemplateParameterObject? Not so much. PowerShell v3 has a nice way to work with JSON objects. Specifically:

 
[sourcecode language='powershell' ]
$imageReferenceObject = ConvertFrom-Json -InputObject $imageReferenceString

But – $imageReferenceObject is a PSCustomObject, not a hashtable, and recall that TemplateParameterObject wants a hashtable. Further, any parameters within TemplateParameterObject that are complex objects must also be hashtables. So we need a few more lines:

 
[sourcecode language='powershell' ]
$imageReference = @{}
$imageReference.Add(“publisher”, $imageReferenceObject.imageReference.publisher)
$imageReference.Add(“offer”, $imageReferenceObject.imageReference.offer)
$imageReference.Add(“sku”, $imageReferenceObject.imageReference.sku)
$imageReference.Add(“version”, $imageReferenceObject.imageReference.version)

And now we can do this:

 
[sourcecode language='powershell' ]
$parameters = @{}
$parameters.Add(“location”, $locationString)
$parameters.Add(“imageReference”, $imageReference)

Cheers!