Deploying Azure VM using PowerShell and CSV

In this post we will be going through the process of using PowerShell to deploy VM’s using the Az module. We will then use a csv file to create multiple VMs.

To start we will need an existing resource group, vnet and subnet that will be use for the VM to be built in and connect the nic.

Next we need to have the Az PowerShell module installed this can be done by using

Install-Module -Name Az -Repository PSGallery -Force

if the module is already installed it a good idea to check if there is any updates we can use the below command to update the module.

Update-Module -Name Az -Force

Once we have the module and pre-req resources in Azure we can start to create the command that will be used to build the VM.

I use the New-AzVM learn page to check on what parameters I wanted to use to create the VM .

We will need to chose a sku size and image to deploy. These can be checked using PowerShell.

For the below exam I will be using a Standard_d2ds image sku to search, we can also search based on cores or memory either.

Get-AzVMSize -Location northeurope | Where-Object {$_.Name -like "Standard_D2ds*"}

Next we need a image for this I will be using Windows server 2022.

Get-AzVMImageSku -Location "northeurope" -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer" | Where-Object {$_.Skus -like "*2022*"}

The image value is made by joining three rows together:

PublisherName:Offer:Skus

for 2022 Azure edition it will be the below.

Now that we have the sku and image we can start to create the command to build the VM.

I will be using splatting as it easier to read when using a lot of parameters.

$vmla = "AzureUser"
$vmlapass = ConvertTo-SecureString "password" -AsPlainText -Force

$vmproperties = @{
    ResourceGroupName = "RG Name"
    Name = "VM Name"
    Location = "AZ Region"
    VirtualNetworkName = "vnet"
    SubnetName = "subnet"
    Size = "sku size"
    Image = "Image"
    Zone = "1"
    Credential =  New-Object System.Management.Automation.PSCredential ($vmla, $vmlapass);
}

Once we have the parameters set we can then call them using

New-AzVm @vmproperties

We can run get-azvm to check the vm is created and running.

To deploy multiple VM’s using a csv file we can use the same splatting we will just be updating to using a foreach loop and import csv.

First we need to create the csv below is the heading that I used.

Next we need to update the parameters splatting to use the variable that will be used in the foreach loop in my case its $vm.

Below is the updated parameters.

Save the script and we will connect to Azure again and run.

The csv and splatting can be updated to add or remove additional parameters.

The only issue I have found is that there is no way to stop a new NSG being create unless you specify an existing NSG. For each VM I deployed it create a new NSG.

I had to manually go in and delete these NSG’s as I am using a NSG on the subnet and don’t want them on the VMs nics.

It looks like in a future release of the Az module you will be allowed to specify no NSG.

https://github.com/Azure/azure-powershell/issues/16890

This can be an useful alterative for deployments of VM’s if you aren’t that familiar with Arm or bicep templates and more comfortable using PowerShell.

Leave a comment