Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass extra parameters along with New-AzureRmResourceGroupDeployment cmdlet

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!

like image 681
CrazyCoder Avatar asked Aug 28 '18 16:08

CrazyCoder


1 Answers

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.

like image 74
Adam Avatar answered Sep 28 '22 06:09

Adam