Home > Enterprise >  Azure CLI - Query Power state of a virtual machine
Azure CLI - Query Power state of a virtual machine

Time:09-21

I would love to query VMs powerstate with azure cli in bash

Mine goal is to stop a virtual machine, but before that check if the machine was already stopped and vise versa.

So I need to get the powerstate of a specific VM, but I don't see az vm list have that as a parameter. How could that be achieved?

CodePudding user response:

I tried to reproduce the same in my environment and got the results successfully like below:

To check Power state of the Azure Virtual Machine, please use the below command:

az vm show -g ResourceGroupName -n testvm -d --query powerState

enter image description here

The command will give the status of the specific Azure VM. And based on that you can Start/Stop the Azure Virtual Machine.

CodePudding user response:

Where is the issue to just try to stop the VM regardless of the state? Adding a check for the state just adds additional processing time for the script.

However, az vm list -d -o table should give you what you are looking for. You could also query for the state directly az vm list -d --query "[?powerState=='VM running']" -o table

The -d or --show-details parameter will add the additional details you are looking for: https://learn.microsoft.com/en-us/cli/azure/vm?view=azure-cli-latest#az-vm-list

  • Related