6

I'm writing a powershell script to create VM using New-AzureRmResourceGroupDeployment cmdlet, which is as below.

New-AzureRmResourceGroupDeployment -Name VmDeployment  `
  -TemplateFile C:\template\template.json `
  -TemplateParameterFile C:\template\parameters.json

This is used create a VM. In parameters.json , there are some parameters like virtualMachineName, networkInterfaceName etc which are hardcoded.
Now I'm trying to automate these scripts , i.e they run on there own from a tool , when ever some condition is met.

My requirement here is , whenever this script runs, it has to increase the number in the VMName . Suppose the VM Name is now VMName1, it has to be VMName2 when the script runs next time. Similarly VMName3 when the script runs next time. Since the virtualMachineName parameter is hardcoded, this is not happening now. Is there anyway I can pass virtualMachineName as a parameter in the script itself rather than taking it from the json file.

Any guidance is highly appreciated.Thanks!

CrazyCoder
  • 2,194
  • 10
  • 44
  • 91

1 Answers1

15

You can definitely do this, and fortunately there are a handful of ways too.

  1. Pass inline parameters. It says in the Azure PowerShell docs for Templates that you can use inline parameters with a local parameter file and the inline parameters take precedence. Relevant paragraph:

You can use inline parameters and a local parameter file in the same deployment operation. For example, you can specify some values in the local parameter file and add other values inline during deployment. If you provide values for a parameter in both the local parameter file and inline, the inline value takes precedence.

This is valuable because it provides you explicit control over the VM Name parameter, but it is up to the caller (you in this case) to pass an inline parameter. Please note this only works with local parameter files and not remote files (i.e. -TemplateParameterFile and not -TemplateParameterUri). The resulting command would look something like:

    New-AzureRmResourceGroupDeployment -Name VmDeployment `
      -TemplateFile C:\template\template.json `
      -TemplateParameterFile C:\template\parameters.json `
      -virtualMachineName VMName42
  1. Modify original parameters.json. You can write some PowerShell/Python/Favorite-scripting-language to parse paramters.json, find the VM Name parameter, find the integer suffix, increment it, and overwrite the file with the new version. This has the benefit of not having to remember to pass an inline parameter, and you won't have to track the version number anywhere as it is already stored in parameters.json. This has one major drawback: it modifies the original JSON which can be dangerous.

  2. Copy parameters.json and modify temporary copy. You can write a script to copy parameters.json to another temporary JSON file and then increment the VM Name parameter during the copy just like in option 2. Pass this temporary file to New-AzureRmResourceGroupDeployment. This has the benefit of not modifying the original parameters.json file but requires you to track the version number somewhere (e.g. another local file, a command line parameter, environment variables, etc.).

For simplicity, I would recommend option 1. It already works out-of-the-box and does not require any external scripts.

st0le
  • 33,375
  • 8
  • 89
  • 89
Adam
  • 2,532
  • 1
  • 24
  • 34
  • this is wrong. you can pass in inline parameters with `templateparameteruri`, there is just no intellisense. overall the temporary parameters file is the best approach. inline parameters are useless if you are doing anything serious – 4c74356b41 Aug 28 '18 at 18:01
  • 1
    I disagree, and so do the [docs](https://learn.microsoft.com/en-us/azure/azure-resource-manager/resource-group-template-deploy#parameter-files): "When you specify a parameter file in the TemplateParameterUri parameter, all inline parameters are ignored." – Adam Aug 29 '18 at 01:42
  • I appreciate you disagreeing, but facts dont care about your feelings (c). `New-AzureRmResourceGroupDeployment -ResourceGroupName xxx -TemplateUri 'https://raw.githubusercontent. com/Azure/azure-quickstart-templates/master/101-vm-simple-windows/azuredeploy.json' -adminusername xxx -adminpassw ord ( ConvertTo-SecureString -Force -AsPlainText '!Qasde32') -dnsLabelPrefix xxx` – 4c74356b41 Aug 29 '18 at 05:28
  • 3
    @4c74356b41 the example in your comment does not have TemplateParameterUri . – Karlo Medallo Dec 06 '18 at 01:45
  • eh? what? it doesnt so what? @KarloMedallo – 4c74356b41 Dec 06 '18 at 05:46
  • 1
    So... your follow up comment does not help your initial claim. – Karlo Medallo Dec 06 '18 at 09:08
  • did you even test it or what @Adam – 4c74356b41 Jan 30 '19 at 10:12
  • 1
    @KarloMedallo is pointing out that your example is not applicable; it does not include the `-TemplateParameterUri` parameter therefore inline parameters are not ignored. No, I did not test it; the burden of proof is not on me since you made the counterclaim but have failed to actually prove your claim. – Adam Feb 11 '19 at 18:49