【问题标题】:How to collect the Azure VM auto-shutdown time using PowerShell?如何使用 PowerShell 收集 Azure VM 自动关机时间?
【发布时间】:2019-05-25 02:56:42
【问题描述】:

我需要使用 PowerShell 收集 Azure VM 自动关闭时间,但不知道如何获取必要的资源属性以便反映自动关闭时间。

我得到以下输出:

ID:  /subscriptions/12345/resourceGroups/W12RG/providers/Microsoft.Compute/virtualMachines/W12

Name                   ResourceGroupName ResourceType                   Location
----                   ----------------- ------------                   --------
shutdown-computevm-W12 W12RG             Microsoft.DevTestLab/schedules eastus1
# Retrieve the resource group information
[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 |  ft
    Write-Host "ID: " $targetResourceId
    $shutdownInformation
}

我需要收集 Azure VM 的自动关闭时间

【问题讨论】:

    标签: powershell azure-powershell


    【解决方案1】:

    您需要将-Expandproperties 开关添加到Get-AzureRMResource 才能访问包含您需要的数据的属性。这将允许您访问.Properties,它将返回一个具有各种其他属性的对象(.dailyRecurrence 给出关闭时间)。关闭时间似乎只是一个由 4 个数字组成的字符串值,前两个数字代表小时,后两个数字代表分钟。所以早上 6:30:45 是 0630,晚上 11:45:55 是 2345。

    [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
    }
    

    我删除了您的 | ft,因为在存储值之前通过格式化程序发送数据通常不是最佳做法。它会改变你的对象,从而改变属性。然后,您以后将无法按预期引用这些属性。如果您想以这种方式显示数据,您可以将其附加到您的单独的 $shutdownInformation 行。也就是说,在你想输出的时候通过format-table发送数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-22
      • 1970-01-01
      • 2017-02-02
      • 2021-02-08
      • 1970-01-01
      • 2017-10-19
      • 2018-10-31
      • 1970-01-01
      相关资源
      最近更新 更多