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

%3CLINGO-SUB%20id%3D%22lingo-sub-2042850%22%20slang%3D%22en-US%22%3EGet%20Azure%20Virtual%20Machine%2FVirtual%20Machine%20Scale%20Set%20which%20are%20stopped%20since%20last%207%20days%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2042850%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EI%20would%20need%20help%20in%20getting%20list%20of%20azure%20virtual%20machine%2Fvirtual%20machine%20scale%20sets%20which%20are%20in%20stopped%20status%20since%20last%207%20days%20so%20I%20can%20drop%20those%20VM%2FVMSS%20to%20save%20the%20cost.%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2042882%22%20slang%3D%22en-US%22%3ERE%3A%20Get%20Azure%20Virtual%20Machine%2FVirtual%20Machine%20Scale%20Set%20which%20are%20stopped%20since%20last%207%20days%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2042882%22%20slang%3D%22en-US%22%3EDo%20we%20have%20any%20log%20analytics%20query%20or%20powershell%20to%20get%20the%20output.%3C%2FLINGO-BODY%3E
New 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.

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