Azure PowerShell – List all VMs that are using a specific VNet subnet

I recently had a query from someone on how-to list all the VMs in a subscription that are part of a subnet. The following script will do this for you:

$vms = Get-AzureVM

 $tgtSubnet = "Subnet-1"

 foreach($vm in $vms)

   $thisVM = Get-AzureVM -ServiceName $vm.ServiceName –Name $vm.Name

   $thisVMSubnet = Get-AzureSubnet -VM $thisVM

   if ($thisVMSubnet -eq $tgtSubnet)
     $thisVM.Name | Out-File C:\Azure\subnet_VM.txt


Leave a Reply

Your email address will not be published. Required fields are marked *