Get Azure Virtual Machine/Virtual Machine Scale Set which are stopped since last 7 days

Copper Contributor

I would need help in getting list of azure virtual machine/virtual machine scale sets which are in stopped status since last 7 days so I can drop those VM/VMSS to save the cost.

3 Replies
Do we have any log analytics query or powershell to get the output.

@mehtaparth09 

 

you can query Azure Resource Graph to find which VMs are currently powered down. There is API/PowerShell/CLI support for Resource Graph. 

resources
| where type =~ 'microsoft.compute/virtualmachines' and properties.extended.instanceView.powerState.code has 'deallocated'

Please note that there is not yet VMSS support for power state. For VMSS, you should query the Azure Resource Manager API.

Having the list of deallocated VMs, you could then query Azure Activity Log for the last deallocate action and thus determine for how long your VM was deallocated.

 

An alternative to this, with a built-in recommendation that gives you VMs that have been deallocated for long, is the Azure Optimization Engine. Please note that you have to let it run at least 2 weeks to get accurate results.

@hspinto 

hi.... is it now possible to use the Azure Resource Graph to get the state of a Scale Set ?

 

you mentioned "Azure Resource Manager API".. how can we do that ?

 

thx!