Jump to content

VMware automation using VixCOM.VixLib


Recommended Posts

Greetings, people of the interweb!

I'm trying to get started on automating actions using the VMware API, but I reached a dead end.  I was trying out this example but it doesn't work for me at all. Line 9, Char 1, Object required: 'VixCOM'.

All that I've done now is got a grasp of how the vmrun works via command line, but I do not find it enough for me.

Could anyone point me at the correct direction or tell me what I'm doing wrong? (Please note that the script is in VBS, I want to get it working first, then continue translating it to AutoIT)

Dim lib
Dim host
Dim job
Dim vm
Dim result

Set lib = CreateObject("VixCOM.VixLib")

' Connect to the local installation of Workstation. This also initializes the VIX API.
Set job = lib.Connect(VixCOM.Constants.VIX_API_VERSION, VixCOM.Constants.VIX_SERVICEPROVIDER_VMWARE_WORKSTATION, Empty, 0, Empty, Empty, 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 lib.ErrorIndicatesFailure(err) Then
   ' Handle the error...
End If

' 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("c:\Virtual Machines\vm1\win2000.vmx", Nothing)
err = job.Wait(Array(VixCOM.Constants.VIX_PROPERTY_JOB_RESULT_HANDLE), results)
If lib.ErrorIndicatesFailure(err) Then
   ' Handle the error...
End If

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(VixCOM.Constants.VIX_VMPOWEROP_LAUNCH_GUI, Nothing, Nothing)
' WaitWithoutResults is just like Wait, except it does not get any properties. 
err = job.WaitWithoutResults()
If lib.ErrorIndicatesFailure(err) Then
   ' Handle the error...
End If

' Wait until VMware Tools are running within the guest, with a 300 second timeout.
Set job = vm.WaitForToolsInGuest(300, Nothing)
err = job.WaitWithoutResults()
If lib.ErrorIndicatesFailure(err) Then
   ' Handle the error...
End If

Set job = vm.LoginInGuest("vixuser", "secret", 0, Nothing)
err = job.WaitWithoutResults()
If lib.ErrorIndicatesFailure(err) Then
   ' Handle the error...
End If

Set job = vm.RunProgramInGuest("c:\myProgram.exe", "/flag arg1 arg2", 0, Nothing, Nothing)
err = job.WaitWithoutResults()
If lib.ErrorIndicatesFailure(err) Then
   ' Handle the error...
End If

Set results = Nothing
Set job = Nothing
Set vm = Nothing

host.Disconnect()

ITypeInfo Viewer showed me that the object name is correct, but the variables are inaccessable using both VixCom object and VIxCOM.VixLib.

Note: https://www.vmware.com/support/developer/vix-api/ SDK from here is also installed on the machine.

I have VMware workstation 10

Any thoughts?

Link to comment
Share on other sites

Interesting.

Can you show what you have at this moment ?

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

Using the API I have nothing, currently I'm looking everywhere, unsuccesfully though. I have commands only using vmrun.exe, using most of the commands available with this app. Example of list command that returns all active VMs

$query=chr(34)&$vmrun_location&chr(34)
   $query=$query&" list"
   $DOS = Run($query, "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
   ProcessWaitClose($DOS)
   $Message = StdoutRead($DOS)
Link to comment
Share on other sites

here you have quick help:

Local $lib
Local $host
Local $job
Local $vm
Local $result

Global Const $VIX_API_VERSION = ????
Global Const $VIX_SERVICEPROVIDER_VMWARE_WORKSTATION = ???
Global Const $VIX_PROPERTY_JOB_RESULT_HANDLE = ???
Global Const $VIX_VMPOWEROP_LAUNCH_GUI = ???

$lib = ObjCreate("VixCOM.VixLib")

; Connect to the local installation of Workstation. This also initializes the VIX API.
$job = $lib.Connect($VIX_API_VERSION, $VIX_SERVICEPROVIDER_VMWARE_WORKSTATION, '', 0, '', '', 0, Null, Null)

; $results needs to be initialized before it's used, even if it's just going to be overwritten.
$results = Null

; 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 $to an array that holds the values for those properties,
; one for each ID requested.
$err = $job.Wait(Array($VIX_PROPERTY_JOB_RESULT_HANDLE), $results)
If $lib.ErrorIndicatesFailure($err) Then
   ; Handle the $error...
EndIf

; The job result handle will be first element in the $results array.
$host = $results(0)

; Open the virtual machine with the given .vmx file.
$job = $host.OpenVM("c:\Virtual Machines\vm1\win2000.vmx", Null)
$err = $job.Wait(Array($VIX_PROPERTY_JOB_RESULT_HANDLE), $results)
If $lib.ErrorIndicatesFailure($err) Then
   ; Handle the $error...
EndIf

$vm = $results(0)

; Power on the virtual machine we just opened. This will launch Workstation if it hasn't
; already been started.
$job = $vm.PowerOn($VIX_VMPOWEROP_LAUNCH_GUI, Null, Null)
; WaitWithoutResults is just like Wait, except it does not get any properties.
$err = $job.WaitWithoutResults()
If $lib.ErrorIndicatesFailure($err) Then
   ; Handle the $error...
EndIf

; Wait until VMware Tools are running within the guest, with a 300 second timeout.
$job = $vm.WaitForToolsInGuest(300, Null)
$err = $job.WaitWithoutResults()
If $lib.ErrorIndicatesFailure($err) Then
   ; Handle the $error...
EndIf

$job = $vm.LoginInGuest("vixuser", "secret", 0, Null)
$err = $job.WaitWithoutResults()
If $lib.ErrorIndicatesFailure($err) Then
   ; Handle the $error...
EndIf

$job = $vm.RunProgramInGuest("c:\myProgram.exe", "/flag arg1 arg2", 0, Null, Null)
$err = $job.WaitWithoutResults()
If $lib.ErrorIndicatesFailure($err) Then
   ; Handle the $error...
EndIf

$results = Null
$job = Null
$vm = Null

$host.Disconnect()
 

not finished here:

Global Const $VIX_API_VERSION = ????
Global Const $VIX_SERVICEPROVIDER_VMWARE_WORKSTATION = ???
Global Const $VIX_PROPERTY_JOB_RESULT_HANDLE = ???
Global Const $VIX_VMPOWEROP_LAUNCH_GUI = ???
.
.
.
$err = $job.Wait(Array($VIX_PROPERTY_JOB_RESULT_HANDLE), $results)
.
.
.
$err = $job.Wait(Array($VIX_PROPERTY_JOB_RESULT_HANDLE), $results)

EDIT:

some script change

EDIT:

remarks: not tested

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

The problem is that I've tried entering those values manually that I've got with ITypeInfo viewer, it is not working due to the fact that $VIX_PROPERTY_JOB_RESULT_HANDLE cannot be used as an INT that I get from the list.. Plus I assume the $VIX_PROPERTY_JOB_RESULT_HANDLE and $VIX_VMPOWEROP_LAUNCH_GUI change each instance of the API is called, as I would be running a few instances of the same API.

Link to comment
Share on other sites

 I've found the problem why this happens, but no idea how to solve this.

 

Are you trying to run the .vbs script directly? You will need to create a .wsf script to invoke the .vbs script. This will ensure that the constants are loaded for the VBScript environment.

The sample code that ships with the VIX installer should give an example .wsf and .vbs pair of files.

 

The content of the .wsf file is

<job id="powerOn">
   <reference object="VixCOM.VixLib" />
   <script language="VBScript" src="powerOn.vbs" />
</job>

How do I make sure that I load the constants from Vixcom.VixLib object?

Link to comment
Share on other sites

Use the powercli...much easier...you can download it, and see all the ready made applets here:

https://www.vmware.com/support/developer/windowstoolkit/wintk40u1/html/

Then there is also a forum, with ton's of other's posted functions to do lots of stuff.  No need to reinvent the wheel.

With this, you can do linked clones, which will spin up a new vm, from a saved snapshot, in less than 15 seconds.

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

  • 3 weeks later...

 I've found the problem why this happens, but no idea how to solve this.

 

The content of the .wsf file is

<job id="powerOn">
   <reference object="VixCOM.VixLib" />
   <script language="VBScript" src="powerOn.vbs" />
</job>

How do I make sure that I load the constants from Vixcom.VixLib object?

 

Still looking for a solution for this.

Link to comment
Share on other sites

You're asking a question regarding VBScript, for an application API that has nothing to do with AutoIT.  It's not a relevant question that anyone here can answer.

This would be best done through vmware forums, or via the API I already pointed you at.

using it, to reproduce your script:

connect-viserver

Get-VM

Wait-VMPowerState -WaitForTools 1

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...