【问题标题】:How to get the list of azure servers having Auto-Shutdown disabled using PowerShell?如何使用 PowerShell 获取禁用自动关机的 azure 服务器列表?
【发布时间】:2021-02-08 12:35:36
【问题描述】:

我想获取禁用了自动关闭的 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 名称及其自动关闭状态显示在屏幕上,以便我轻松找出哪些所有 VM 当前已禁用自动关闭。

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

【问题讨论】:

    标签: azure powershell azure-powershell powershell-4.0 azure-rm


    【解决方案1】:

    尝试这样的方法来获取所有虚拟机的自动关闭状态。与其尝试在循环中获取计划,不如获取订阅中的所有计划并根据 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
    }
    

    【讨论】:

    • @ SamaraSoucy-MSFT,执行代码后出现此错误,知道吗? Get-AzureRmResource :无法使用指定的命名参数解析参数集。在 C:\Demo3.ps1:28 char:25 + ... ormation = (Get-AzureRmResource -ResourceType Microsoft.DevTestLab/sc ... + ~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -AzureRmResource], ParameterBindingException + FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.GetAzureResourceCmdlet
    • 查看您的 AzureRM 版本-Get-InstalledModule -Name AzureRM -AllVersions。我不得不将我的更新到最新(6.13.1),因为此命令在版本 5 中存在问题。
    【解决方案2】:

    您只需要使用以下方法获取 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 阅读更多相关信息。

    【讨论】:

    • @RoadRunner 谢谢,我想知道你在 foreach 循环之前使用的 & 有什么用?以及如何将输出复制到控制台上显示的文本文件?我尝试在末尾添加设置内容,例如 Format-Table -AutoSize | Set-Content C:\Output1.txt 但它没有用,然后我也尝试在没有 format-table 命令的 foreach 循环之后添加 set-content,它工作但以这种格式复制了内容 @{VMName=vwmazdvag1 ;关机状态=启用}。其中包含其他数据,而不仅仅是 VM 名称及其状态。
    • 另外,对于某些 VM,我的状态为 ShutdownStatus=ResourceNotFound :找不到资源组“ResourceGroupName”下的资源“microsoft.devtestlab/schedules/shutdown-computevm-vwmazimdvdb1”。更多详情请至aka.ms/ARMResourceNotFoundFix}。由于 RG 是订阅的一部分,因此对此错误有任何想法吗?
    • @SRP 我添加了& 运算符的解释。至于将整个输出传送到文件,您可以使用Format-Table -AutoSize | Out-File -FilePath output.txt。取决于您想要的输出格式,这不包含在您的问题中。
    • extra 信息可能来自Get-AzResource 引发异常,因此我将$_.Exception.Message 用于在状态中显示错误消息。 ResourceNotFound 会发生,因为您的 VM 没有启用关闭状态,这本身就是 microsoft.devtestlab/schedules 资源。您可能可以输入其他内容,但输入异常消息可能更有助于找出 VM 没有关闭状态的原因。
    猜你喜欢
    • 2020-04-21
    • 1970-01-01
    • 2020-03-18
    • 2016-06-12
    • 1970-01-01
    • 2017-01-30
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    相关资源
    最近更新 更多