Azure Powershell How long VM has been Stopped (deallocated)

olegarr 131 Reputation points
2019-12-10T19:46:29.75+00:00

Hello:

I am looking for an advice for the best way to find out how long VM has been Stopped (deallocated) via Powershell.

Is it possible?

As of now the only idea I have is to use metrics and see when CPU is less (or more) than "0", but this way does not seem as really good approach to me.

Thanks a lot!

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

2 answers

Sort by: Most helpful
  1. Manoj Reddy 411 Reputation points Microsoft Employee
    2019-12-11T11:28:19.877+00:00

    @olegarr You can configure custom alerts or use alerts from metrics to trigger whenever a machine is deallocated.

    However, if you want to do this through PS, you can run the following command to list all the VM's and the timestamp of the action that triggered the deallocation.

    Get-AzLog -Status Accepted -DetailedOutput | ?{$_.Authorization.Action -eq "Microsoft.Compute/virtualMachines/deallocate/action"} | fl ResourceId,EventTimestamp

    This command searches for the status "succeeded" in all the deallocate action which are occurring under the resource provider "Microsoft.Compute" and lists the timestamp and resource ID you need.

    You can then the use that to calculate the timeframe.

    1 person found this answer helpful.

  2. RANJEET KUMAR 0 Reputation points
    2024-04-12T13:18:13.84+00:00

    To Check when was the Azure VM stopped last time, run below command:

    az disk show --name <OS_disk_name> --resource-group <RG_Name> --subscription <Subscription_Name>

    where OS_disk_name is disk attached to VM

    and then look for "LastOwnershipUpdateTime" property.

    0 comments No comments

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.