Deploy Azure VM w/PowerShell and Az-Module


As I promised in my previous article, today we’ll write PowerShell script to deploy VM in the Microsoft Azure cloud.

Before you begin

  • The script utilizes several functions from my Azure Automation Az-Module. The Az-Module contains auxiliary functions for the Microsoft AzureRm Module, you will need it too.

  • Every time you run the script it deploys single VM from a JSON template. For this example and learning purposes I choose a template that deploys VM from existing VHD (virtual disk file), but you will be able to adjust the script to suite to any other template.

  • The deep dive to Azure Resource Manager templates is out of scoop of this article. For those of you who are not familiar with the JSON templates can read this excellent article.


  • The login to your Azure environment must be done outside the script! Use Login-AzureRmAccount cmdlet or my New-AzCredProfile function.

  • By and large we can divide the process to three stages: taking user input, creating JSON parameter file and executing deployment job. Let’s see all these steps in a detail.

Stage 1: User input

  • The purpose of this stage is to collect all necessary data: the resource group name for deployment and values to initialize all template’s parameters (”parameters”: { } scope). The template itself is saved in the $TemplateJson variable, edit it according to your environment.

  • To get user input the script uses its parameters and Select- functions from the Az-Module.

Get-Help .\Deploy-AzureVmVhd.ps1 -Parameter *
Get-Command -Verb select -Module Az-Module
  • All Select- functions are based on the Write-Menu function. The Write-Menu is included in the Az-Module.
Get-Help Select-AzObject –Full
Get-Help Select-AzSubscription
  • To determine next free IP address the Get-AzSubnet function is used.

Stage 2: Create JSON parameters file

  • The JSON parameters file is created by New-AzParamsJson function, that you already know from the previous article.


  • All parameter files are named as VM name and placed in the folder, saved in the $JsonPath variable. Edit this variable according to your environment. There is possible to use these parameter files in the future for your reference or to redeploy your resources.

Stage 3: Deploy VM

  • The last stage runs job to deploy your resources by New-AzureRmResourceGroupDeployment cmdlet from AzureRm module. All it needs for successful deployment are two JSON files (template -TemplateFile and parameters file -TemplateParameterFile) and resource group name -ResourceGroupName.
  • Why resources and not VM? Actually our template defines two resources: VM and vmnic.

  • The resources are defined in the ”resources”: [ { }, { } ] scope of the template files, each resource is enclosed by curly braces { }.

  • In the Azure object hierarchy virtual network card is not child object of a Virtual Machine Emoj. A vmnic may exist alone and you can link/unlink it to any VM, moreover, when you delete your VM, its linked vmnic is not deleted!

Write your own script

  • As you can see, a deployment script is not flexible enough because it strictly depends on particular resource template Emoj. But there is good news, once you understand the process you can adjust the script to match any templates.

  • For example, to deploy VM from this template instead of the previous one, you will need to define one more variable $dataDisk1VhdUri, initialize it by a script parameter and add it to the $ParamsHash hash table of your new script. That script will be able to deploy VM with one additional data disk.

$ParamsHash = @{
‘dataDisk1VhdUri’ = $dataDisk1VhdUri;
  • The script supports -Confirm parameter and requires to confirm your intension unless you specify -Confirm:$false.
.\Deploy-AzureVmVhd.ps1 -Envi Test -VMName azvm1 -OSDiskVhd
.\Deploy-AzureVmVhd.ps1 Prod azvm2 -Confirm:$false



  • I had an ulterior motive to choose just this type of resource template that defines VM to be deployed from existing VHD. It intended to assist to system administrators in any migration scenario.
  • Just create a VHD on premise by tool of your choice and upload it to any Storage Account. All the rest of the work on the Azure side will be done by the script.

  • Good luck in your automation and you are welcome to add your comments. In an upcoming article we will automate such VHD operations like copying, renaming, etc.

You might also like

Azure VM Tag automation
How to login to the Azure automatically
How to find orphaned VHD files in the Azure

One thought on “Deploy Azure VM w/PowerShell and Az-Module

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s