如何使用 PowerShell 获取禁用自动关机的 azure 服务器列表?

How to get the list of azure servers having Auto-Shutdown disabled using PowerShell?

我想获取已禁用自动关闭的 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 的输出按以下格式显示,

我想要的很简单,我想要在屏幕上显示虚拟机名称和自动关机状态,以便我很容易找出哪些虚拟机当前禁用了自动关机。

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

尝试类似这样的操作来获取所有 VM 的 auto-shutdown 状态。不要尝试在循环中获取计划,而是获取订阅中的所有计划并根据 VM 的完整资源 ID 匹配它们。

[array]$VMArray = Get-AzureRMVm | Select-Object -Property ResourceGroupName, Name, VmId, Id
$ShutdownInformation = (Get-AzureRmResource -ResourceType Microsoft.DevTestLab/schedules -Expandproperties).Properties

foreach($vm in $VMArray) {
    $ShutdownStatus = "Not Configured"
    $Schedule = $ShutdownInformation | Where-Object { $_.targetResourceId -eq $vm.Id } | Select -First 1
    if($Schedule -ne $null) {
        $ShutdownStatus = $Schedule.status
    }

    Write-Host $vm.VmId $ShutdownStatus
}

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

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

然后使用 Get-AzVM, Get the microsoft.devtestlab/schedules resource using Get-AzResource, then output VM name and status into a table using Format-Table.

遍历所有 VM
$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 模块,因为对 AzureRm 的支持将于 2020 年 12 月结束。您可以阅读 documentation 了解更多相关信息。

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

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

更新

Call operator & is used here to run the for loop as a script block. You can read more about this in about_Script_Blocks.