Q. How can I check the status of Hyper-V virtual machine (VM) heartbeat health through a script?

A. When Hyper-V integration services are enabled in a VM, a heartbeat is available to make sure the virtual OS is responsive. You can check this heartbeat using a script by modifying the script from the previous FAQ, now named listvmshb.vbs. Note that the objVMService calls a different command from the previous FAQ, and the heartbeat status is added to the end of the command.

' lsitvmshb.vbs
' John Savill 5/19/2011
intArrInfoToGet = Array(0,1,100,104) ' Name, ElementName, EnabledState and Heartbeat
                                     ' http://msdn.microsoft.com/en-us/library/cc160706(VS.85).aspx

For count = 0 To (WScript.Arguments.Count-1)
strHVServer = WScript.Arguments.Item(count)
Set objWMIService = GetObject("winmgmts:\\" & strHVServer & "\root\virtualization")
Set objVMService = objWMIService.ExecQuery("SELECT * FROM Msvm_VirtualSystemManagementService").ItemIndex(0)

Call objVMService.GetSummaryInformation(NULL,intArrInfoToGet,arrVMs)

For Each VM In arrVMs
strState = "Unknown"
strHB = "Unknown"
Call convertVMStateToText(VM.EnabledState,strState)
Call convertVMHBToText(VM.HeartBeat,strHB)
WScript.StdOut.WriteLine VM.ElementName & "|" & strHVServer & "|" & strState & "|" & strHB

Set VM = Nothing
Set arrVMs = Nothing

sub convertVMStateToText(ByVal varEnabledState, inStrVMState) 'ByVal for the state as cannot change it but want to update the inStrVMState
select case varEnabledState
case 0
inStrVMState = "unknown"
case 2
inStrVMState = "running"
case 3
inStrVMState = "powered off"
case 32768
inStrVMState = "paused"
case 32770
inStrVMState = "starting"
case 32773
inStrVMState = "saving"
case 32774
inStrVMState = "stopping"
case 32776
inStrVMState = "pausing"
case 32777
inStrVMState = "resuming"
case else
inStrVMState = "don't know"
end select
end Sub

sub convertVMHBToText(ByVal varHeartBeat, inStrVMHB) 'ByVal for the state as cannot change it
select case varHeartBeat
case 2
inStrVMHB = "OK"
case 6
inStrVMHB = "Error"
case 12
inStrVMHB = "No Contact"
case 13
inStrVMHB = "Lost Communication"
End select
end sub

To call the script, I would just use

cscript listvbshb.vbs <Hyper-V server 1> <Hyper-V server 2>

For example,

D:\projects\VBScripts> cscript listvms.vbs savdalbfs01.savilltech.net savdalbfs02

Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

APPVSequence|savdalbfs01.savilltech.net|powered off|Unknown
PXETest|savdalbfs01.savilltech.net|powered off|Unknown
suseentdesk|savdalbfs02|powered off|Unknown
savloncli02|savdalbfs02|powered off|Unknown
Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.