简体   繁体   English

如何使用 Excel 工作表中的 powershell 从 Azure 获取虚拟机状态的特定列表

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

I am pretty much new to PowerShell and we have customer requirement that they will share Azure VM details in Excel sheet with below columns.我对 PowerShell 非常陌生,我们有客户要求他们将在 Excel 工作表中与以下列共享 Azure VM 详细信息。

Excel 工作表值

we have to get VM Status details from all the subscriptions & ResourceGroup using the Powershell script.我们必须使用 Powershell 脚本从所有订阅和资源组中获取 VM 状态详细信息。

Outuput:输出:

在此处输入图片说明

I am able to perform for single RSG and VM values by using the below code我可以使用以下代码执行单个 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

I can't test this myself, but you will have to create nested loops to get the details for all subscriptions and resourcegroups.我自己无法对此进行测试,但您必须创建嵌套循环才能获取所有订阅和资源组的详细信息。

Something like this:像这样的东西:

$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