gpt4 book ai didi

powershell - 列出属于特定子网的所有 azure vm

转载 作者:行者123 更新时间:2023-12-03 03:15:12 25 4
gpt4 key购买 nike

我想列出特定子网中的所有 Azure 虚拟机。我不知道如何为此编写 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
}
}
}

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

最佳答案

请注意,在 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

关于powershell - 列出属于特定子网的所有 azure vm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28985282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com