01-08-2021 12:54 AM
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.
01-08-2021 12:57 AM
01-09-2021 03:17 PM
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.