Forum Discussion

mehtaparth09's avatar
mehtaparth09
Copper Contributor
Jan 08, 2021

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

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.

    • hspinto's avatar
      hspinto
      Icon for Microsoft rankMicrosoft

      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.

      • pingpinglah's avatar
        pingpinglah
        Copper Contributor

        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!

Resources