0
votes

I have a script that deallocates all VMs in the subscription based on the tags assigned - off hours and start them back up the next day using Jenkins. I want to be able to query these VMs based on the state (Running/Stopped(deallocated) and output it to a file.

Startup command - az vm start --ids $(az resource list --tag Restart=${TAG_RESTART} --query "[?type=='Microsoft.Compute/virtualMachines'].id" -o table)

Query command - az resource list --tag Restart=yes --query "[].{Name:name,Group:resourceGroup,Location:location}" -o table

This command returns output (Name, RG and location). I want it to also show the powerstate and possibly OS type once the restart script is complete. If it is also possible to export the output to a spreadsheet.

1
Tried this az vm list --ids $(az resource list --tag Restart='yes' --query "[].{Name:name,Group:resourceGroup,Location:location,PowerState:powerState=='running'}" -o table) Got this error az : ERROR: az: error: unrecognized arguments: --ids Name Group Location ------------- ------------- ---------- hazrnv000er01 dvops-prod-rg eastus At line:1 char:1 + az vm list --ids $(az resource list --tag Restart='yes' --query "[].{ ... + CategoryInfo : NotSpecified: (ERROR: az: erro...prod-rg eastus:String) [], RemoteExcepti on + FullyQualifie..bonafide

1 Answers

0
votes

You could use az vm show -d --ids to get powershell state.

Sorry, I don't have a Mac VM. On Linux VM, I use the following command to get it.

az vm show  -d --ids $(az resource list --tag Restart=shui --query "[?type=='Microsoft.Compute/virtualMachines'].id"|jq -r ".[]") --query 'powerState'

On Mac, maybe you could use the following command.

az vm show -d --ids $(az resource list --tag Restart=${TAG_RESTART} --query "[?type=='Microsoft.Compute/virtualMachines'].id" -o table) --query 'powerState'

You could get help by using az vm show -h

--show-details -d  : Show public ip address, FQDN, and power states. command will run slow.