1

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

  • `'...'` strings are _verbatim_ strings in PowerShell, for string interpolation you need `"..."` (e.g., `"$test"`). However, to pass a variable value in isolation - even if it contains spaces - you don't need quoting at all, so simply use `$test` – mklement0 Jul 26 '23 at 22:11
  • 1
    As well as @mklement0's comment about quotes and string interpolation, if you want to pass a value into the ARM Template you need to pass it in the ```-TemplateParameterObject``` parameter - something like ```-TemplateParameterObject @{ "vmName" = $test }```. The ```-Name``` parameter you're currently specifying is the name of the Deployment run, not a value for the ```vmName``` parameter inside the template. Have a read of the https://learn.microsoft.com/en-us/powershell/module/az.resources/new-azresourcegroupdeployment?view=azps-10.1.0#description for more details... – mclayton Jul 26 '23 at 23:39
  • 1
    Thank you so much to everyone @mklement0 that is extremely useful to know and I appreciate the better understanding – EmployeeBoy Jul 27 '23 at 20:59
  • 1
    Thank you @mclayton for the information and documentation it was extremely helpful and I was able to get everything resolved, I appreciate the detail added and the references you made to the code I was working with, made it a lot easier to understand. Been a busy day at work so I havent been able to fully read that documentation yet but I'll be sure to get back to it tmrw, thank you again – EmployeeBoy Jul 27 '23 at 21:00

2 Answers2

1

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

RithwikBojja
  • 5,069
  • 2
  • 3
  • 7
  • Huge thanks @Rithwik for the visualization and putting it in a use-case definitely gave me the complete understanding and a good reference point as well. I was able to get it functioning following everyone's guidance. thank you guys so much! – EmployeeBoy Jul 27 '23 at 20:57
1

thanks again to Rithwik, Mclayton, and Mklement really made everything come together, wish I could give it a thumbs up but stackoverflow is silly and wont let me because I am a noob acc. Any who here's the solution:

$test = "vmNAMEtest"

New-AzResourceGroupDeployment `
    -ResourceGroupName 'test' `
    -Name $test `
    -TemplateUri '.\Documents\azuredeploy2.json' `
    -TemplateParameterObject @{ "vmName" = $test }

Nothing had to be done on the JSON template side just keeping it how it is and having the place holder as a backup. Only thing I had to change was on the powershell script side. Adding TemplateParameterObject line and following what Mclayton said for the answer or visually what Rithwik said agreeing to Mclayton. One issue I was coming across when doing this was I forgot to add "`" at the end of the TemplateUri line - not allowing the code to reach the TemplateParameterObject line

This script is extremely helpful when populating Azure subscriptions with Azure resources through powershell

Sorry imma noob, but thanks to everyone mentioned for all your help and kindness