skip to Main Content

To display the state (Running, Deallocated etc) of our VM’s, I currently use the following command and this works fine.

az vm list -o table -d -g ....

With the number of VM’s increasing on a regular basis, this is getting very cumbersome. For 200 VM’s, I need to run this command 200 times.

Is there an easier way to obtain this information, by querying GraphAPI maybe ?

2

Answers


  1. I have reproduced in my environment and Thanks to @volody, followed his SO-thread and below is the query to run in Azure Resource Graph explorer:

          Resources
            | project name, location,
            PowerState=tostring(properties.extended.instanceView.powerState.code),
    type
            | where type =~ 'Microsoft.Compute/virtualMachines'
            | order  by name desc ```
    

    enter image description here

    References taken from:

    Edit 2:

    Yes you can get different vms using names as below:
    I used has :

    | where name has "x" or name has "y"

    enter image description here

    Login or Signup to reply.
  2. You can use the "–query" parameter to get the names of VMs which have a particular text in names(At the start). The ! is ignoraing the VMs which names start with ‘E2E’.

    $vmsinrg=$(az vm list -g $resourcegroup --query "[?!starts_with(name,'E2E')].{ VMName:name }") | Convertfrom-json
    

    There are other conditions as well which are taken by –query.
    [https://learn.microsoft.com/en-us/cli/azure/query-azure-cli?tabs=concepts%2Cbash][1]

    If you do not have some naming rules which you can use to get the names of those 200 VMs then you need to think about using a naming convention as you cant do any thing except do them manually.

    What you can also do is create the list one time, everytime you have a new VM add the new name to that list. import that list as an array and then run the command to get the status of each one at a time using a loop.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search