当前尝试获取我所有订购中的所有VM及其VNet的报告,最好是电子表格。类似于在虚拟机主页的门户中显示的方式,但是复制和粘贴非常草率且没有结构。
我尝试通过遍历所有VM尝试使用PowerShell,然后拉出VM名称和RG以拉出Nic,然后从Nic中获得包含Vnet的子网。但是,我不确定这种逻辑是否完全正确。下面的PS打印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"
}
如果可以通过VNet排序的所有订阅中的所有VM的电子表格的获取路径完全简单,我可以接受它,因为这似乎过于繁琐。另外,如果我可以在VNets中获得数量众多的VM(不是NICS),这些数量也可以达到我的最低最终目标。.请提供任何帮助!
答案 0 :(得分: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。从技术上讲,您不需要所有变量,因为您可以计算哈希表中的值。