angelcervera
angelcervera

Reputation: 4199

Unable to deploy Azure ARM with parameters using az cli from powershell script

Trying simple deployment with parameters from a PS script:

$prefix = "xxx"
$location = "switzerlandnorth"
az deployment group create `
    --name  $timestamp  `
    --resource-group $resourceGroupName `
    --mode incremental `
    --verbose `
    --template-file .\src\ops\scripts\arm.json `
    --parameters "{ `"location`": { `"value`": `"$location`" },`"projectPrefix`": { `"value`": `"$prefix`" } }"


Response with error:

Unable to parse parameter: { location: { value: switzerlandnorth }, projectPrefix: { value: xxx } }

Running from a PS1 script.

Upvotes: 0

Views: 3207

Answers (3)

Zach Johnson
Zach Johnson

Reputation: 810

I'll throw in my two cents here because previous answers are slim on what we are actually doing here. In short the az deployment group create command wants the --parameters argument to be a double serialized JSON string {scoff}. In order to create a value that can be handled by az deployment group create we would need to do something like this:

$stupidlyFormattedParam = @{
  someAppSetting1 = @{ value = "setting 1" }
  someAppSetting2 = @{ value = "setting 2" }
} | ConvertTo-Json -Compress -Depth 10 | ConvertTo-Json

az deployment group create --name someName--resource-group someGroup --parameters $stupidlyFormattedParam --template-file .\someFile.json

Upvotes: 1

bmoore-msft
bmoore-msft

Reputation: 8737

Another way you can think of this is using objects/hashtables in PS and then converting to JSON, e.g.

$param = @{
  location = "switzerlandnorth"
  prefix = "foo"
}

$paramJSON = ($param | ConvertTo-Json -Depth 30 -Compress).Replace('"', '\"') # escape quotes in order to pass the command via pwsh.exe

az deployment group create -g $resourceGroupName--template-file "azuredeploy.json" -p $paramJson

This is a similar example: https://gist.github.com/bmoore-msft/d578c815f319af7eb20d9d97df9bf657

Upvotes: 1

Jagrati Modi
Jagrati Modi

Reputation: 2088

As we can see in the error that it is unable to parse the parameters. The correct way to pass the parameters to the az deployment group create command is:

 az deployment group create `
 --name  $timestamp  `
 --resource-group $resourceGroupName `
 --mode "incremental" `
 --verbose `
 --template-file ".\src\ops\scripts\arm.json" `
 --parameters '{ \"location\": { \"value\": \"switzerlandnorth\" },\"projectPrefix\": { \"value\": \"xxx\" } }' 

Update:

If you want to pass the PowerShell variables in the Parameters you can do something like below -

 $location = "switzerlandnorth"
 $projectPrefix = "xxx"
 $params = '{ \"location\": { \"value\": \" ' + $location + '\" },\"projectPrefix\": { \"value\": \"' + $projectprefix + '\" } }' 
 az deployment group create `
 --name  $timestamp  `
 --resource-group $resourceGroupName `
 --mode "incremental" `
 --verbose `
 --template-file ".\src\ops\scripts\arm.json" `
 --parameters $params

Hope this helps!

Upvotes: 1

Related Questions