Q. How can I check the status of Hyper-V virtual machine (VM) heartbeat health through a script?
June 5, 2011
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 & "rootvirtualization")
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
Next
Set VM = Nothing
Set arrVMs = Nothing
Next
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
For example,
D:projectsVBScripts>cscript listvms.vbs savdalbfs01.savilltech.net savdalbfs02
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
SAVDALBFS01|savdalbfs01.savilltech.net|running|OK
savdalync01|savdalbfs01.savilltech.net|running|OK
savdalclient.savilltech.net|savdalbfs01.savilltech.net|running|OK
APPVSequence|savdalbfs01.savilltech.net|powered off|Unknown
savdalappv01|savdalbfs01.savilltech.net|running|OK
PXETest|savdalbfs01.savilltech.net|powered off|Unknown
savdalts01|savdalbfs01.savilltech.net|running|OK
savdalex10|savdalbfs01.savilltech.net|running|OK
savdalrodc01|savdalbfs02|running|OK
savdalclient3.savilltech.net|savdalbfs02|running|OK
savdaldc10|savdalbfs02|running|OK
suseentdesk|savdalbfs02|powered off|Unknown
savloncli02|savdalbfs02|powered off|Unknown
savdalclient4.savilltech.net|savdalbfs02|running|OK
About the Author
You May Also Like