Pass hashtable as parametwr to ARM template from PowerShell

Viewed 1159

I would like to replace my parameter file and just give the parameter with powershell. My parameters file looks like this:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "subnets": {
      "value": {
        "settings": [
          {
            "name": "firstSubnet",
            "addressPrefix": "10.0.0.0/24"
          },
          {
            "name": "secondSubnet",
            "addressPrefix": "10.0.1.0/24"
          }
        ]
      }
    }
  }
}

Which I try to override with:

-subnets {"settings":[{"name": "firstSubnet","addressPrefix": "10.0.0.0/24"},{"name": "secondSubnet","addressPrefix": "10.0.1.0/24"]}}

But I keep getting errors. What is the correct syntax for using hash table as parameter?

1 Answers

Your example is not a hash table. It's just an unquoted JSON string.

Hash tables are defined in PowerShell as

@{}

and arrays are

@()

So you'd be looking at constructing something that looks like this:

-subnets @{ 
             settings = @(
                 @{ 
                     name = 'foo'
                     address = 'bar'
                  }
             ) 
          }

and so on.

You can use ConvertFrom-Json and ConvertTo-Json to convert JSON to/from PowerShell hash tables and arrays.

Related