Search code examples
jsonpowershellvariablesscriptingazure-powershell

Change JSON variables dynamically with a powershell script


The following is a test script i am running trying to have the name change on the JSON file. Below is my PowerShell script. I would like my "$test" variable to pass through to the JSON file but it is staying

Powershell Script PS 5.1

$test = "vmNAMEtest"

New-AzResourceGroupDeployment `
    -ResourceGroupName 'test' `
    -Name '$test' `
    -TemplateUri '.\Documents\azuredeploy.json'

JSON

"vmName": {
        "type": "string",
        "defaultValue": "simple-vm",
        "metadata": {
          "description": "Name of the virtual machine."
        }
      },

Everytime the script is automatically the defaultValue "simple-vm" but i'd like to pass the $test value

Some things I've tried: in the JSON code:

"defaultValue": "$test" - this did not work

"defaultValue": "'+$test+'" - also no luck


Solution

  • I have reproduced in my environment, I do agree with @mclayton and below are my expected results:

    $test = "rithwik000000"
    
    New-AzResourceGroupDeployment `
        -ResourceGroupName 'rbojja' `
        -Name $test `
        -TemplateUri 'C:\Users\rbojja\Downloads\rith.json' `
        -TemplateParameterObject @{ "LogicAppName" = $test }
    

    Output:

    enter image description here