How to list the Azure VMs from the Availability set using PowerShell?


To list the Azure VMs under the availability set, we can use the command as shown below

Before running the command make sure that you are connected to the Azure Cloud (if not use ConnectAzAccount) and proper azure subscription (Set-AzContext to set the Azure subscription)

$availabilitySetVMs =
Get-AzAvailabilitySet -ResourceGroupName MYRESOURCEGROUPAVAILABILITY -Name
myAvailabilitySet

To get the total number of Virtual machines in the availability set,

PS C:\> $availabilitySetVMs.VirtualMachinesReferences.id.count

Output

To get all the VM names from the list, we can use the below command.

$VMlist = Get-AzAvailabilitySet -ResourceGroupName
MYRESOURCEGROUPAVAILABILITY -Name myAvailabilitySet

$i=0

foreach($vm in $VMlist.VirtualMachinesReferences){
   "VM{0}: {1}" -f $i,($vm.Id.Split('/'))[-1]
   $i++
}

Output

Updated on: 02-Sep-2021

431 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements