如何使用PowerShell获取禁用了自动关闭功能的Azure服务器列表?

问题描述

我想获取禁用了自动关闭功能的azure服务器的列表,我有以下脚本,但是脚本的问题是它在Subscription 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

每个虚拟机的输出以以下格式显示

enter image description here

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

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

解决方法

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

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

然后使用Get-AzVM遍历所有VM,使用Get-AzResource获取microsoft.devtestlab/schedules资源,然后使用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 &作为脚本块运行for循环。您可以在about_Script_Blocks中了解有关此内容的更多信息。

,

尝试类似的方法以获取所有VM的自动关闭状态。不必尝试获取循环内的调度,而是获取预订中的所有调度并根据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
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...