bonafide
bonafide

Reputation: 368

How to query VMs in Azure powerstate using tags

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.

Upvotes: 0

Views: 2986

Answers (1)

Shui shengbao
Shui shengbao

Reputation: 19223

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.

Upvotes: 1

Related Questions