簡體   English   中英

如何使用 Excel 工作表中的 powershell 從 Azure 獲取虛擬機狀態的特定列表

[英]How to get specifi list of Virtual Machine Status from Azure using powershell from Excel sheet

我對 PowerShell 非常陌生,我們有客戶要求他們將在 Excel 工作表中與以下列共享 Azure VM 詳細信息。

Excel 工作表值

我們必須使用 Powershell 腳本從所有訂閱和資源組中獲取 VM 狀態詳細信息。

輸出:

在此處輸入圖片說明

我可以使用以下代碼執行單個 RSG 和 VM 值

 $SubscriptionName = Get-AzSubscription -SubscriptionId $subscriptionId $RG = "rgp-use2-prd-bioportalbiopeople1" $RSGName = Get-AzResourceGroup -Name $RG $VMs = Get-AzVM -Name "vmbppapiv1prd02" $VMState = (Get-AzVM -Name $VM -ResourceGroupName $RG -Status).Statuses $vmOutput = $VMs | ForEach-Object { [PSCustomObject]@{ "Resource Group Name" = $RSGName.ResourceGroupName "Subscription Name" = $SubscriptionName.Name "VM Name" = $_.Name "VM Type" = $_.StorageProfile.osDisk.osType "VM Statss" = ($VMState | where code -Like 'PowerState/*')[0].DisplayStatus } } $vmOutput | Format-Table -AutoSize $vmOutput | export-csv C:\\Projects\\data.csv

我自己無法對此進行測試,但您必須創建嵌套循環才能獲取所有訂閱和資源組的詳細信息。

像這樣的東西:

$subscriptions = Get-AzSubscription -TenantId "aaaa-aaaa-aaaa-aaaa"  # enter the tenant ID here
$VMs = Get-AzVM -Name "vmbppapiv1prd02"

$vmOutput = foreach ($vm in $VMs) {
    foreach ($subscription in $subscriptions) {
        Set-AzContext -SubscriptionId $subscription.Id
        (Get-AzResourceGroup).ResourceGroupName | ForEach-Object {
            $vmState = (Get-AzVM -Name $vm.Name -ResourceGroupName $_ -Status).Statuses
            [PSCustomObject]@{
                "Resource Group Name" = $_
                "Subscription Name"   = $Subscription.Name
                "VM Name"             = $vm.Name
                "VM Type"             = $vm.StorageProfile.osDisk.osType
                "VM Status"           = ($vmState | where code -Like 'PowerState/*')[0].DisplayStatus
            }
        }
    }
}

$vmOutput | Format-Table -AutoSize 
$vmOutput | Export-Csv -Path 'C:\Projects\data.csv' -NoTypeInformation

暫無
暫無

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

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