首页 > 解决方案 > 如何使用 PowerShell 获取禁用自动关机的 azure 服务器列表?

问题描述

我想获取禁用了自动关闭的 azure 服务器列表,我有以下脚本,但脚本的问题是它获取订阅 GUID 下的 RG 列表,但在每个循环后重复输出。

Import-AzureRmContext -Path "$PSScriptRoot\AzureProfile.json"

Select-AzureRmSubscription -SubscriptionId {subscriptionId}


[array]$ResourceGroupArray = Get-AzureRMVm | Select-Object -Property ResourceGroupName, Name, VmId
   
foreach ($resourceGroup in $ResourceGroupArray){
    $targetResourceId = (Get-AzureRmVM -ResourceGroupName $resourcegroup.ResourceGroupName -Name $resourceGroup.Name).Id
    $shutdownInformation = (Get-AzureRmResource -ResourceGroupName $resourcegroup.ResourceGroupName -ResourceType Microsoft.DevTestLab/schedules -Expandproperties).Properties
    Write-Host "ID: " $targetResourceId
    $shutdownInformation

每个 VM 的输出以以下格式显示,

在此处输入图像描述

我想要的很简单,我希望虚拟机名称及其自动关闭状态显示在屏幕上,以便我轻松找出所有虚拟机当前已禁用自动关闭。

与此相关的任何帮助都会有所帮助。

标签: azurepowershellazure-powershellpowershell-4.0azure-rm

解决方案


您只需要microsoft.devtestlab/schedules使用以下方法获取资源 ID:

/subscriptions/{subscriptionId}/resourceGroups/{rgName}/providers/microsoft.devtestlab/schedules/shutdown-computevm-{vmName}

然后使用 遍历所有 VM Get-AzVM,使用 获取microsoft.devtestlab/schedules资源Get-AzResource,然后使用 将 VM 名称和状态输出到表中Format-Table

$subscriptionId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

Set-AzContext -SubscriptionId $subscriptionId

& {
    foreach ($vm in Get-AzVM) {
        try {
            $shutdownResource = Get-AzResource `
                -ResourceId "/subscriptions/$subscriptionId/resourceGroups/$($vm.ResourceGroupName)/providers/microsoft.devtestlab/schedules/shutdown-computevm-$($vm.Name)" `
                -ErrorAction Stop

            [PSCustomObject]@{
                VMName = $vm.Name
                ShutdownStatus = $shutdownResource.Properties.status
            }
        }
        catch {
            [PSCustomObject]@{
                VMName = $vm.Name
                ShutdownStatus = $_.Exception.Message
            }
        }
    }
} | Format-Table -AutoSize

要将上下文设置为正确的订阅,我们可以使用Set-AzContext.

然而,上面使用的是最新的Az模块。您可以使用等效AzureRm模块执行相同操作。

$subscriptionId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

Set-AzureRmContext -SubscriptionId $subscriptionId

& {
    foreach ($vm in Get-AzureRmVM) {
        try {
            $shutdownResource = Get-AzureRmResource `
                -ResourceId "/subscriptions/$subscriptionId/resourceGroups/$($vm.ResourceGroupName)/providers/microsoft.devtestlab/schedules/shutdown-computevm-$($vm.Name)" `
                -ErrorAction Stop

            [PSCustomObject]@{
                VMName = $vm.Name
                ShutdownStatus = $shutdownResource.Properties.status
            }
        }
        catch {
            [PSCustomObject]@{
                VMName = $vm.Name
                ShutdownStatus = $_.Exception.Message
            }
        }
    }
} | Format-Table -AutoSize

尽管我确实建议移至该Az模块,因为对 2020 年 12 月的支持AzureRm将结束。您可以阅读文档以获取有关此的更多信息。

上面的代码应该给你一个类似于下面的输出

VMName    ShutdownStatus
------    --------------
vm1       Enabled
vm2       Disabled

更新

调用运算符 &在这里用于将 for 循环作为脚本块运行。您可以在 中阅读有关此内容的更多信息about_Script_Blocks


推荐阅读