Search code examples
azureazure-cli

Azure CLI - Query Power state of a virtual machine


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?


Solution

  • 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.