如何使用PowerShell收集Azure VM自动关闭时间? [英] How to collect the Azure VM auto-shutdown time using PowerShell?

查看:123
本文介绍了如何使用PowerShell收集Azure VM自动关闭时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to collect the Azure VM auto-shutdown time using PowerShell, but don't know how to get to the necessary resource property so the auto-shutdown time is reflected.

我得到以下输出:

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的自动关闭时间

I need to collect the auto-shutdown time for the Azure VM

推荐答案

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

You need to add the -Expandproperties switch to Get-AzureRMResource to gain access the properties that contain the data you need. This will allow you to access .Properties, which will return an object with various other properties (.dailyRecurrence gives the shutdown time). The shutdown time appears to just be a string value of 4 numbers with the first two numbers representing the hour and last two being the minutes. So 6:30:45 AM would be 0630, and 11:45:55 PM would be 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发送数据.

I removed your | ft since it is generally not best practice to send data through the formater before storing the value. It will change your object and therefore change the properties. You then won't be able to reference those properties as expected later. If you want to display data that way, you can just append that to your lone $shutdownInformation line. In other words, send data through format-table at the time you want to output.

这篇关于如何使用PowerShell收集Azure VM自动关闭时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆