question

olegarr avatar image
0 Votes"
olegarr asked amanpreetsingh-msft action

Azure Powershell How long VM has been Stopped (deallocated)

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

KAREDD-MSFT avatar image
1 Vote"
KAREDD-MSFT answered YagnavalkyaSivaPrasadManchi-4627 commented

@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.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@KAREDD-MSFT ,


Thank you very much for your reply! I'll check it out and come back.


Thanks again!


0 Votes 0 ·

@KAREDD-MSFT ,


Thanks again for your help!


I tried the command you recommended, but looks like it's pulling data from the log only for short period of time (looks like last 6 hours), but I want to know time when VM was turned OFF even if it happened 60 days ago.


Is it possible?


Thank you!


0 Votes 0 ·

@olegarr It is possible. You just have to use the Switch -Starttime along with the command. The command can pull logs from up to 90 days.

Take a look at the examples listed here for how to use different switches along with Get-Azlog command




0 Votes 0 ·