列出属于特定子网的所有Azure虚拟机 [英] Listing all azure vm belonging to particular subnet

查看:59
本文介绍了列出属于特定子网的所有Azure虚拟机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想列出特定子网中的所有Azure虚拟机.我不知道如何为此编写PowerShell脚本.我正在尝试以下操作,但未提供所需的输出.我想要的输出-属于IP地址为10.87.00.01 ... 10.87.99.99的子网的所有VM都应在我的文本文件subnet_VM.txt中列出

I want to list all azure vms in a particular subnet. I don't know how to write a PowerShell script for this. I am trying the below but it's not giving the desired output. My desired output- All VMs belonging to subnet having ip 10.87.00.01...10.87.99.99 should get listed in my text file subnet_VM.txt

$tgtIP = "10.87.xx.xx"
$vms = Get-AzureVM

foreach($vm in $vms)
{
  $vmIP = (Get-AzureVM -ServiceName $vm.ServiceName  –Name  $vm.Name).IpAddress
  foreach($ip in $vmIP)
  {
    if($ip -like '$tgtIP*')
    {
       $vm.Name > C:\AZURE\subnet_VM.txt
    }
  }
}

如果我对此有所帮助,将非常有帮助.预先感谢.

It would be very helpful if I get help on this. Thanks in advance.

推荐答案

请注意,在Azure中,子网具有名称.因此,一个更优雅的解决方案是使用子网名称而不是原始IP范围.由于这是您最终要实现的目标,因此我也包括了该解决方案.

Note that in Azure the subnets have a name. So a more elegant solution would be to use a subnet name than the raw IP range. Since that is something you are ultimately trying to achieve, I am including that solution as well.

$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)
    {
        $output  =$output + "`r`n" + $thisVM.Name 
    }
}
$output | Out-File C:\Azure\subnet_VM.txt

这篇关于列出属于特定子网的所有Azure虚拟机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆