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

CMD does not support UNC paths as current directories. Use PUSHD instead.

25 April 2006 20:41 — Tips & Tricks,Windows

CMD.exe does not allow changing the current directory to a UNC path. If you try, it complains that "CMD does not support UNC paths as current directories.".

PUSHD to the rescue:

C:\\> pushd \\\\serendipity\\D$
Z:\\>

   Do something here with the files on Z:

Z:\\> popd
C:\\> _

PUSHD temporarily maps a drive letter to the specified UNC share (starting from Z: on down until it finds one that's available). Until you call POPD, you can access the share via that drive letter (also from other DOS boxes and Windows Explorer).

Sure beats mapping and unmapping shares all the time, but keep in mind that the PUSHD trick only works if you don't need to specify credentials to access the share.

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