How To Check That A Hyper-V VM Is Active Using PowerShell

I wanted to write a little bit of code to see if a virtual machine was active or not.  Here is a crude bit of code that you could turn into a function:

$VM = Get-VMIntegrationService -VMName VM04 -Name Heartbeat
while ($VM.PrimaryStatusDescription -ne "OK")
    $VM = Get-VMIntegrationService -VMName VM04 -Name Heartbeat
    write-host "The VM is not on"
    sleep 5

    write-host "The VM is on"

The code checks to see if the integration component for the VM heartbeat is active.  This assumes you have either the Windows Integration Components or the Linux Integration Services installed (I wrote this code testing with a Ubuntu 12.04 VM with the built-in services) and that you have not disabled the Integration Services in the VM properties.

The code simply queries the heartbeat status to see if it is “OK” or not.  It will loop until the status is “OK”.  You could use it to see when a VM is active … it is actually testing to see when the Integration Components/Services are active and responsive.

The code could be more elegant, and could be turned into a function for reuse.  This is just a crude example to get you started.

4 thoughts on “How To Check That A Hyper-V VM Is Active Using PowerShell”

  1. Hey Aidan,

    Wouldn’t the following cmdlets be easier to test where a VM was Running or not?

    > Get-VM | Where State -eq ‘Running’
    > Get-VM | Where State -eq ‘Off’


    1. Hey Hans,

      I believe that’ll tell us if the VM is running (including just started up) but checking the integration component heartbeat will tell us if the OS is running … the IC is a service.

      1. Get-VM and Get-VMIntegrationService both use Get-WMIObject under the covers anyway. You can use Get-VM $VMName and select the Heartbeat as well.

Leave a Reply to Heather Simon Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.