Getting vitual machine last state date

Assaf L 287 Reputation points
2022-11-06T12:01:27.367+00:00

When querying for a virtual machines states, a list of InstanceViewStatus (link) called statuses is received
The first state, which is usually ProvisioningState/succeeded contains a time field
The next status item which can be with code PowerState/deallocated or PowerState/running is missing the Time field

Is there a way of getting the second Time value or is it by design?

This powershell command will return the data with the missing fields:
Get-AzureRmVm | Get-AzureRmVm -Status

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
9,044 questions
0 comments No comments
{count} votes

Accepted answer
  1. Arun Siripuram 911 Reputation points
    2022-11-06T18:15:36.987+00:00

    @Assaf L

    As per my analysis, Statuses[0].Time parameter signifies the VM was stopped\deallocated\running time as there is no explicit parameter for DisplayStatus/PowerState : VM deallocated/running. Please find the scripts below.

    Note: replace resourcegroupname and virtualmachinename accordingly

    Get-AzVM -ResourceGroupName "Resourcegroupname" -Name "VirtualMahinename" -Status

    Note: No changes
    Get-AzureRmVm | Get-AzureRmVm -Status | select ResourceGroupName, name, @{n="Provisioned Time"; e={$.Statuses[0].Time}}, @{n="Provisioned Status"; e={$.Statuses[0].DisplayStatus}}, @{n="Running Status"; e={$_.Statuses1.DisplayStatus}}}

    for more information, please refer: https://learn.microsoft.com/en-us/powershell/module/az.compute/get-azvm?view=azps-9.1.0

    Attached snapshots FYR

    257598-vitual-machine-last-state-date-1.png
    257653-vitual-machine-last-state-date-2.png

    Please Accept the answer if the information helped you. This will help us and others in the community as well.


1 additional answer

Sort by: Most helpful
  1. JimmySalian-2011 42,511 Reputation points
    2022-11-06T15:15:40.623+00:00

    Hi,

    You can check the PS code over here in SOF forum and it has provided a script to pull out the stats if that helps? azure-powershell-command-to-get-the-uptime-for-all-vms

    Hope this helps.
    JS

    ==
    Please Accept the answer if the information helped you. This will help us and others in the community as well.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.