Need VMWare VIX Automation Tools and SDK
Code:
'Reference
'C:\Windows\SysWOW64\regsvr32.exe ***\VixCOM64.dll
'Reference VixCOM64.dll TO vb6 Project
Dim lib As VixCOM.VixLib
Dim vmPath As String
Private Sub Form_Load()
vmPath = "***/Windows 7.vmx"
' 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 results
'Dim lib
Dim job
Dim host
Dim vm As IVM2
Dim err
Dim useWorkstation
Dim hostType
Dim hostName
Dim hostUsername
Dim hostPassword
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 '=3
hostName = Empty
hostUsername = Empty
hostPassword = Empty
'vmPath = "***/Windows 7.vmx"
poweronOption = VixCOM.Constants.VIX_VMPOWEROP_LAUNCH_GUI '=512
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)
If err <> 0 Then 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)
If CLng(err) <> 0 Then QuitIfError (err)
Set vm = results(0)
'MsgBox TypeName(vm)
' 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()
If CLng(err) <> 0 Then QuitIfError (err)
'MsgBox "正在启动,启动完成后,点确定按钮"
MsgBox "Doing Start Vmware virtual machine,When Start Successfull,CLICK OK BUTTON", vbOKOnly
' 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.
'If MsgBox("是否关闭?", vbYesNo) = vbYes Then
If MsgBox("Do You Want to Close Vmware virtual machine?", vbYesNo) = vbYes Then
Set job = vm.PowerOff(VixCOM.Constants.VIX_VMPOWEROP_NORMAL, Nothing)
err = job.WaitWithoutResults()
If CLng(err) <> 0 Then QuitIfError (err)
host.Disconnect
End If
'MsgBox "测试完成"
MsgBox "TestOk"
End Sub
Sub QuitIfError(errID)
On Error GoTo DoErr
MsgBox "errID:" & CLng(errID)
' If lib.ErrorIndicatesFailure(err) Then
' WScript.Echo ("Error: " & lib.GetErrorText(err, Empty))
' WScript.Quit
' End If
Exit Sub
DoErr:
MsgBox err.Description
End Sub