List all the vm in a particular Subnet in Azure

佐手、 提交于 2021-01-29 07:17:43

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!