【问题标题】:Listing all azure vm belonging to particular subnet列出属于特定子网的所有 azure vm
【发布时间】:2015-03-11 11:20:55
【问题描述】:

我想列出特定子网中的所有 azure vm。我不知道如何为此编写 PowerShell 脚本。我正在尝试以下,但它没有提供所需的输出。我想要的输出 - 属于具有 ip 10.87.00.01...10.87.99.99 的子网的所有虚拟机都应该列在我的文本文件 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
    }
  }
}

如果我能得到这方面的帮助,那将非常有帮助。提前致谢。

【问题讨论】:

  • 如果你改变这个$tgtIP = "10.87"会有帮助吗?目前你正在尝试将一个 IP 与 x.x 匹配,这显然永远不会工作
  • @arco44 - 那也没用
  • 您需要编辑问题以 a) 描述所需的输出 b) 准确解释什么“不起作用”
  • 我按照@arco044 的建议尝试了 $tgtIP = "10.87" ,但没有奏效。我已经在问题部分编辑了我想要的输出

标签: powershell azure powershell-ise azure-powershell


【解决方案1】:

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

$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

【讨论】:

猜你喜欢
  • 2021-11-03
  • 2015-09-22
  • 1970-01-01
  • 2019-03-01
  • 2018-03-18
  • 1970-01-01
  • 2022-08-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多