How to collect the Azure VM auto-shutdown time using PowerShell?

不想你离开。 提交于 2019-12-11 16:44:48

问题


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.

I am getting the following output:

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
}

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


回答1:


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
}

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.



来源:https://stackoverflow.com/questions/56295205/how-to-collect-the-azure-vm-auto-shutdown-time-using-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!