簡體   English   中英

如何獲得有關我的Azure VM及其VNet的報告?

[英]How can I get a report of my Azure VMs with their VNets?

當前正在嘗試獲取我所有訂購中的所有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),這些數量也可以達到我的最低最終目標。.任何幫助將不勝感激!

您可以執行以下操作:

$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。 從技術上講,您不需要所有變量,因為您可以計算哈希表中的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM