Arnout's Eclectica

But I digress…

Using VmCOM to display the status of VMware Server guests

29 April 2006 15:17 — VMware,Windows

VmCOM is a COM-based API that allows you to control VMware Server from your own software. As an example, the following script displays the status of each registered guest:

Option Explicit
Dim cp, server, vmCollection, vmName, vm, s

Const vmErr_VMBusy = &H80040215
Const vmExecutionState_On = 1
Const vmExecutionState_Off = 2
Const vmExecutionState_Suspended = 3
Const vmExecutionState_Stuck = 4

Set cp = CreateObject("VmCOM.VmConnectParams")
Set server = CreateObject("VmCOM.VmServerCtl")

server.Connect cp
Set vmCollection = server.RegisteredVmNames

For Each vmName in vmCollection
   Set vm = CreateObject("VmCOM.VmCtl")
   On error resume next
   vm.Connect cp, vmName
   If Err.Number = vmErr_VMBUSY Then
      s = Basename(vmName) & ":      BUSY"
   ElseIf Err.Number <> 0 Then
      s = Basename(vmName) & ":     ERROR"
   Else
      On Error Goto 0
      s = vm.Config("displayName") & ": " & State2Str(vm)
   End if
   WScript.Echo s
next


Function State2Str(ByVal vm)
  Select Case vm.ExecutionState
      Case vmExecutionState_On
         State2Str = "       ON"
      Case vmExecutionState_Off
         State2Str = "      OFF"
      Case vmExecutionState_Suspended
         State2Str = "SUSPENDED"
      Case vmExecutionState_Stuck
         State2Str = "    STUCK"
      Case else
         State2Str = "  UNKNOWN"
   End Select
End Function


Function Basename(ByVal path)
   Dim pos
   pos = InstrRev(path, "\")
   If pos > 0 Then
      Basename = Mid(path, pos + 1)
   Else
      Basename = path
   End If
End Function

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URI

Sorry, the comment form is closed at this time.

Copyright © 2006-2009 Arnout Grootveld — Powered by WordPress — Hosted at pair Networks