' 
' Copyright 2006 VMware, Inc.
' All rights not expressly granted to you by VMware, Inc. are reserved.
'  

' VixCOM VBScript Sample Script (powerOn)
'
' This demonstrates how to open a VM, power it on and power it off.
'
' This uses the Wait function to block after starting each
' asynchronous function. This effectively makes the asynchronous
' functions synchronous, because Wait will not return until the
' asynchronous function has completed.
'
' Instructions for Windows 2000 and later operating systems:
'
'  - there should be an accompanying file named 'powerOn.wsf'
'    It is placed in the same directory as this file during
'    product installation. This file is responsible for setting
'    up the Windows Script Host environment and loading the
'    VixCOM type library, thereby enabling this script to 
'    reference symbolic constants such as VIX_API_VERSION
'
'  - in a command line window, type:
'    cscript //nologo powerOn.wsf
' 

Dim lib 
Dim job
Dim host
Dim vm
Dim err
Dim useWorkstation
Dim hostType
Dim hostName
Dim hostUsername
Dim hostPassword
Dim vmPath
Dim poweronOption

' Certain arguments differ when using VIX with VMware Server 2.0 and
' VMware Workstation.
'
' Comment out this line to use this code with VMware Server 2.0.
useWorkstation = 1

If useWorkstation Then
   hostType = VixCOM.Constants.VIX_SERVICEPROVIDER_VMWARE_WORKSTATION 
   hostName = Empty
   hostUsername = Empty
   hostPassword = Empty
   vmPath = "C:\VMs\winxppro\winxppro.vmx"
   poweronOption = VixCOM.Constants.VIX_VMPOWEROP_LAUNCH_GUI
Else
   ' For VMware Server 2.0
   hostType = VixCOM.Constants.VIX_SERVICEPROVIDER_VMWARE_VI_SERVER
   hostName = "https://192.20.30.40:8333/sdk"
   hostUsername = "Administrator"
   hostPassword = "password"
   vmPath = "[standard] winxppro/winxppro.vmx"
   poweronOption = VixCOM.Constants.VIX_VMPOWEROP_NORMAL
End If

Set lib = CreateObject("VixCOM.VixLib")

' Connect to the local installation of Workstation. This also intializes the VIX API.
Set job = lib.Connect(VixCOM.Constants.VIX_API_VERSION, hostType, hostName, 0, hostUsername, hostPassword, 0, Nothing, Nothing)

' results needs to be initialized before it's used, even if it's just going to be overwritten.
Set results = Nothing

' Wait waits until the job started by an asynchronous function call has finished. It also
' can be used to get various properties from the job. The first argument is an array
' of VIX property IDs that specify the properties requested. When Wait returns, the 
' second argument will be set to an array that holds the values for those properties,
' one for each ID requested.
err = job.Wait(Array(VixCOM.Constants.VIX_PROPERTY_JOB_RESULT_HANDLE), results)
QuitIfError(err)

' The job result handle will be first element in the results array.
Set host = results(0)

' Open the virtual machine with the given .vmx file.
Set job = host.OpenVM(vmPath, Nothing)
err = job.Wait(Array(VixCOM.Constants.VIX_PROPERTY_JOB_RESULT_HANDLE), results)
QuitIfError(err)

Set vm = results(0)

' Power on the virtual machine we just opened. This will launch Workstation if it hasn't
' already been started.
Set job = vm.PowerOn(poweronOption, Nothing, Nothing)
' WaitWithoutResults is just like Wait, except it does not get any properties. 
err = job.WaitWithoutResults()
QuitIfError(err)

' Here you would do any operations on the guest inside the virtual machine.

' Power off the virtual machine. This will cause Workstation to shut down if it
' was not running previous to the call to PowerOn.
Set job = vm.PowerOff(VixCOM.Constants.VIX_VMPOWEROP_NORMAL, Nothing)
err = job.WaitWithoutResults()
QuitIfError(err)

host.Disconnect()


'--------------------------------------------------------------------------------
' A simple error handler. Prints the error message to the console, and then exits.
sub QuitIfError(err)
   if lib.ErrorIndicatesFailure(err) then
      WScript.Echo("Error: " & lib.GetErrorText(err, empty))
      WScript.Quit
   end if
end sub