how to get Azure VM last reboot using azure resource graph

672 Views Asked by At

I'm using azure resource graph to create dashboard and need the VM last reboot or Power-Off date. Need your helps please. Thank you

1

There are 1 best solutions below

1
On

I tried to reproduce the same in my environment:

Graph query:

Resources
| where type == 'microsoft.compute/virtualmachines'
| summarize count() by PowerState = tostring(properties.extended.instanceView.powerState.code)

Checked the powerstate : enter image description here

Tried below query :

resources
| where type has 'microsoft.compute/virtualmachines/extensions' 
| where name has 'MicrosoftMonitoringAgent' or name has 'AzureMonitorWindowsAgent' 
| extend AzureVM = extract('virtualMachines/(.*)/extensions',1,id), ArcVM = extract('machines/(.*)/extensions',1,id)
|summarize count() by name=tolower(AzureVM), ArcVM=tolower(ArcVM), subscriptionId, resourceGroup,  AgentType=name
| extend hasBoth = iff(count_ > 0, 'Yes', 'No') 
| join 
 ( 
    resources
    | where type =~ 'Microsoft.Compute/virtualMachines'
    | project name, properties.extended.instanceView.powerState.displayStatus,
                    properties.extended.instanceView.powerState.code,
                    created_ = properties.timeCreated
    | order by name desc
 ) on name

where i got created time of azure vm running and deallocation time.

enter image description here

If you want the alert when the vm stpped you can check this : azureportal - Azure alert to notify when a vm is stopped - Stack Overflow

Reference: resource-graph-samples | Microsoft Learn