List Azure VMs from 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: 2021-09-02T11:50:46+05:30

542 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements