问题
Just wondering how to retrieve the VMs details in a given particular subnet in Azure.
I have used the below but it does not work anymore in powershell (Cloud shell)
Listing all azure vm belonging to particular subnet
Any help will be appreciated.
Thanks
回答1:
The following code snippet has been tested against my environment :
$vms = Get-AzVM
$tgtSubnet = "MySubnet"
foreach($vm in $vms)
{
$networkInterface = Get-AzNetworkInterface -Name ($vm.NetworkProfile.NetworkInterfaces[0].Id.Split('/')[-1])
$subnetName = $networkInterface.IpConfigurations[0].Subnet.Id.split('/')[-1]
if($subnetName -eq $tgtSubnet)
{
Write-Output $vm
}
}
output:
Make sure Azure PowerShell is installed on your machine.
来源:https://stackoverflow.com/questions/64257408/list-all-the-vm-in-a-particular-subnet-in-azure