如何获取我的Azure VM及其所属VNets的报告?

4
目前正在尝试获取一份报告,最好是电子表格形式的,其中包含所有订阅中所有虚拟机及其虚拟网络(VNet)。与门户网站在虚拟机主页中呈现方式类似,但复制粘贴出来非常混乱和无结构性。
我尝试使用 PowerShell 逐个遍历所有虚拟机,然后提取 VM 名称和 RG 来获取 Nic,再从 Nic 获取包含 Vnet 的子网。然而,我不确定这个逻辑是否完全正确。下面的 PowerShell 命令打印了VM名称、VM资源组、VM Nic名称、Nic子网和Nic VM,请问这个命令是否合理?
$vms   = Get-AzureRmVM

Echo "VM Name,VM Resource Group,VM NIC Name,VM Subnet,VM VNet"

foreach ($vm in $vms){

 $thisvmName = $vm.Name
 $thisvmRG = $vm.ResourceGroupName
 $thisvmNicName = $vm.NetworkProfile.NetworkInterfaces.Id.Split("/")[8]

 $thisvmNic = Get-AzureRmNetworkInterface -Name $thisvmNicName -ResourceGroupName $thisvmRg
 $thisvmNicIPConfig = Get-AzureRmNetworkInterfaceIpConfig -NetworkInterface $thisvmNic
 $thisvmNicSubnet = $thisvmNicIpConfig.Subnet.Id.Split("/")[10]
 $thisvmNicVNet = $thisvmNicIPConfig.Subnet.Id.Split("/")[8]

 echo "$thisvmName,$thisvmRG,$thisvmNicName,$thisvmNicSubnet,$thisvmNicVNet"

}

如果有一种完全更容易的方法来获取我在所有订阅中的所有VM的电子表格,并且我可以按VNet进行排序,那么我会开放接受,因为这似乎相当过度了。此外,如果我可以获取VNets中VM数量(而不是NICS),那么对于我的最小目标可能也有用..非常感谢任何帮助!
1个回答

1
你可以做如下操作:

你可以这样做:

$vms   = Get-AzureRmVM
$output = foreach ($vm in $vms){

 $thisvmName = $vm.Name
 $thisvmRG = $vm.ResourceGroupName
 $thisvmNicName = $vm.NetworkProfile.NetworkInterfaces.Id.Split("/")[8]

 $thisvmNic = Get-AzureRmNetworkInterface -Name $thisvmNicName -ResourceGroupName $thisvmRg
 $thisvmNicIPConfig = Get-AzureRmNetworkInterfaceIpConfig -NetworkInterface $thisvmNic
 $thisvmNicSubnet = $thisvmNicIpConfig.Subnet.Id.Split("/")[10]
 $thisvmNicVNet = $thisvmNicIPConfig.Subnet.Id.Split("/")[8]
 [pscustombject]@{
    'VM Name'= $thisvmName
    'VM Resource Group'= $thisvmRG
    'VM NIC Name'= $thisvmNicName
    'VM Subnet'= $thisvmNicSubnet
    'VM VNet' = $thisvmNicVNet
}

$output | Export-Csv -Path C:\CSV.csv -NoTypeInformation

这假设您对存储在变量中的数据感到满意。唯一的更改是在每次循环迭代中创建一个自定义对象,添加属性名称和相关值。这些对象存储在数组($output)中,并在最后导出为CSV。您不一定需要所有变量,因为可以在哈希表中计算值。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接