' Scripting Library for VMware GSX server
' Saturday, 2002-08-31
' Constants at top are derived via
' Michael Harris' TypeLib Extractor
' Content following developed against
' VMware GSX Server 2.01 build-2129
' http://www.vmware.com
'


'======================================
'
'  Description: VMware VmCOM 1.0 Type Library
'
'         Name: VMCOMLib
'      Version: 1.0 [of Type Library]
'         GUID: {0407CAC2-41D7-4EB4-898B-4EC06C57BB81}
'
'         File: VmCOM.dll
'      Version: 1.0.0.1 [of file]
'
' Extracted on: 2002.08.28 00:53:08
'
'=====================================
' VmErr
'=====================================
Const vmErr_ALREADYCONNECTED = -2147220980
Const vmErr_BADRESPONSE = -2147220978
Const vmErr_BADSTATE = -2147220984
Const vmErr_BADVERSION = -2147220986
Const vmErr_DISCONNECT = -2147220979
Const vmErr_GARBAGE = -2147220977
Const vmErr_INSUFFICIENT_RESOURCES = -2147220970
Const vmErr_INVALIDARGS = -2147220989
Const vmErr_INVALIDVM = -2147220974
Const vmErr_NEEDINPUT = -2147220976
Const vmErr_NETFAIL = -2147220990
Const vmErr_NOACCESS = -2147220988
Const vmErr_NOEXECVMAUTHD = -2147220983
Const vmErr_NOMEM = -2147220991
Const vmErr_NOPROPERTY = -2147220982
Const vmErr_NOSUCHVM = -2147220981
Const vmErr_NOTCONNECTED = -2147220987
Const vmErr_NOTSUPPORTED = -2147220975
Const vmErr_PROXYFAIL = -2147220969
Const vmErr_TIMEOUT = -2147220985
Const vmErr_UNSPECIFIED = -2147219993
Const vmErr_VMBUSY = -2147220971
Const vmErr_VMEXISTS = -2147220972
Const vmErr_VMINITFAILED = -2147220973
'=====================================
' VmExecutionState
'=====================================
Const vmExecutionState_Off = 2
Const vmExecutionState_On = 1
Const vmExecutionState_Stuck = 4
Const vmExecutionState_Suspended = 3
Const vmExecutionState_Unknown = 5
'=====================================
' VmPlatform
'=====================================
Const vmPlatform_LINUX = 2
Const vmPlatform_UNKNOWN = 4
Const vmPlatform_VMNIX = 3
Const vmPlatform_WINDOWS = 1
'=====================================
' VmPowerOpMode
'=====================================
Const vmPowerOpMode_Hard = 1
Const vmPowerOpMode_Soft = 2
Const vmPowerOpMode_TrySoft = 3
'=====================================
' VmProdInfoType
'=====================================
Const vmProdInfo_Build = 3
Const vmProdInfo_Platform = 2
Const vmProdInfo_Product = 1
Const vmProdInfo_Version_Major = 4
Const vmProdInfo_Version_Minor = 5
Const vmProdInfo_Version_Revision = 6
'=====================================
' VmProduct
'=====================================
Const vmProduct_ESX = 3
Const vmProduct_GSX = 2
Const vmProduct_UNKNOWN = 4
Const vmProduct_WS = 1
'=====================================
' VmTimeoutId
'=====================================
Const vmTimeoutId_Default = 1



'=====================================
'=====================================
'
' Wrapper Procedures for Vmware GSX
'
'
'=====================================

Function GetLocalVM(path)
 ' Returns an object reference to a local VM
 ' takes the VMX file's path as its argument
 Dim cnx, vm_server, vm
 ' Set connection object reference
 Set cnx = CreateObject("VmCOM.VmConnectParams")
 ' Set VMware server reference
 Set vm_server = CreateObject("VmCOM.VmServerCtl")
 On Error Resume Next
 vm_server.Connect cnx
 if Err.number <> 0 then Exit Function
 Set vm = CreateObject("VmCOM.VmCtl")
 vm.Connect cnx, path
 if Err.Number <> 0 then Exit Function
 On Error Goto 0
 Set GetLocalVM = vm
End Function 

Function GetRemoteVM(path, Host, User, Password, Port)
 ' Returns an object reference to a remote VM
 ' path - VMX file's path
 ' Host - address/name of remote host
 ' User - valid user who can connect to remote console
 ' Password - user's password
 ' (to avoid embedding, you can use input to get this
 ' dynamically)
 ' Port - port on which to connect.  If you have not
 '  customized this on the server, should be 902	
 Dim cnx, vm_server, vm
 Set cnx = CreateObject("VmCOM.VmConnectParams")
 cnx.hostname = Host
 cnx.username = User
 cnx.password = Password
 'Following logic allows use of "" for default port
 If Port<> "" Then cnx.port = Port
 Set vm_server = CreateObject("VmCOM.VmServerCtl")
 On Error Resume Next
 vm_server.Connect cnx
 if Err.number <> 0 then Exit Function
 Set vm = CreateObject("VmCOM.VmCtl")
 vm.Connect cnx, path
 if Err.Number <> 0 then Exit Function
 On Error Goto 0
 Set GetRemoteVM = vm
End Function 

Function RegisteredVMs
 ' Returns an array containing all local
 ' Registered VM paths
 Dim server, vm, i, aTmp()
 Redim aTmp(-1)
 set server = CreateObject("VmCOM.VmServerCtl")  
 server.Connect CreateObject("VmCOM.VmConnectParams")
 For Each vm in server.RegisteredVmNames
  Redim Preserve aTmp(UBound(aTmp) + 1)
  aTmp(UBound(aTmp)) = vm
 Next
 RegisteredVMs = aTmp
End Function

Function vm_PowerState(vm)
 ' Returns power state of a VMware machine
 ' takes a VM object ref as an argument
 If TypeName(vm) <> "IVmCtl" Then Exit Function
 vm_PowerState = vm.ExecutionState
End Function

Function vm_PowerOn(vm)
 ' Powers on a VM if it is not already on
 ' Returns "True" if it is able to power it on
 ' takes a VM object ref as an argument	
 Dim State0
 On Error Resume Next
 vm_PowerOn = False
 State0 = vm.ExecutionState
 If (State0 = vmExecutionState_Off) OR _
  (State0 = vmExecutionState_Suspended) Then 
  vm.Start vmPowerOpMode_Soft
  if Err.Number = 0 Then vm_PowerOn = True
 End If
 On Error Goto 0
End Function

Function vm_PowerOffSoft(vm)
 ' Powers off a VM if it is already on
 ' Returns "True" if it is able to power it off
 ' attempts soft powerdown
 ' takes a VM object ref as an argument	
 Dim State0
 On Error Resume Next
 vm_PowerOffSoft = False
 State0 = vm.ExecutionState
 If (State0 = vmExecutionState_On) Then 
  vm.Stop vmPowerOpMode_Soft
  if Err.Number = 0 Then vm_PowerOffSoft = True
 End If
 On Error Goto 0
End Function
VMware Scripting Wrappers