New-AzResourceGroupDeployment -TemplateParameterObject

Joseph Durnal 321 Reputation points
2020-09-14T14:01:06.537+00:00

I am having trouble deploying a VM using New-AzResourceGroupDeployment -TemplateParameterObject

When using -TemplateParameterFile, everything works:

New-AzResourceGroupDeployment -ResourceGroupName test -TemplateFile .\azuredeploy.json -TemplateParameterFile .\azuredeploy.parameters.json -adminPassword (ConvertTo-SecureString -Force -AsPlainText 'xxxxxxxxxxxxxxxx') -whatif

When I put the JSON into a hashtable and try to use it with -TemplateParameterObject, it doesn't work:

$a = Get-Content .\azuredeploy.parameters.json | ConvertFrom-Json -AsHashtable
New-AzResourceGroupDeployment -ResourceGroupName test -TemplateFile .\azuredeploy.json -TemplateParameterObject $a.parameters -adminPassword (ConvertTo-SecureString -Force -AsPlainText 'xxxxxxxxxxxxx') -whatif

New-AzResourceGroupDeployment:
InvalidTemplate - Long running operation failed with status 'Failed'. Additional Info:'Deployment template validation failed: 'Template parameter JToken type is not valid. Expected 'String, Uri'. Actual 'Object'. Please see https://aka.ms/resource-manager-parameter-files for usage details.'.'

The strings, etc, are all correct in the JSON but something breaks when I put it into a hashtable. Everything is in the hashtable that should be, maybe I'm just not using it correctly.

Thanks,
Joe

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,120 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Stanislav Zhelyazkov 21,101 Reputation points MVP
    2020-09-15T06:57:36.207+00:00

    Hi,
    Also make sure to use Az 4.6.X at least as there are some improvements on the parameters you use for that command specifically:

    https://github.com/Azure/azure-powershell/releases

    Fixed serialization error for '-TemplateObject' and 'TemplateParameterObject' [#1528] [#6292]

    2 people found this answer helpful.

  2. Winston 2,761 Reputation points
    2020-09-15T05:09:02.647+00:00

    Hi @Joseph Durnal ,

    Thanks for the question. I've tried to reproduce your error but had to make some assumptions. I'm assuming your files are based off of /azuredeploy.json and /azuredeploy.parameters.json files respectively. I'd need to see a snippet of your JSON to help further. I'd also like to point out that it looks like your using PowerShell 6 or above as the -AsHashtable option didn't exist prior. For your convince I've gotten it to work in PowerShell 7 with the -AsHashtable option but please keep in mind that currently the default version of PowerShell installed with Windows is PowerShell 5 which doesn't have that option. To do this with the default version installed on Windows 10 you would need to write your own -AsHashtable function like this. Just wanted to point that out incase someone tries this later on and is confused. With the information I have at hand it should work as:

    (1) $a = Get-Content .\azuredeploy.parameters.json | ConvertFrom-Json -AsHashtable
    (2) New-AzResourceGroupDeployment -ResourceGroupName test -TemplateFile .\azuredeploy.json -TemplateParameterObject $a.parameters

    Can you please share the .json files you are using so I can try to repro with your specific files?

    1 person found this answer helpful.