Jump to content
Sign in to follow this  
Second

VMware automation using VixCOM.VixLib

Recommended Posts

Second

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?

Share this post


Link to post
Share on other sites
mLipok

Interesting.

Can you show what you have at this moment ?


Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest begining - comunication with GitHub REST API Forum Rules *
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for 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
 

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 *

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 * 

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 TaskScheduler

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 *

"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: 2018-10-31

Share this post


Link to post
Share on other sites
Second

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)

Share this post


Link to post
Share on other sites
mLipok

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:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest begining - comunication with GitHub REST API Forum Rules *
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for 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
 

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 *

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 * 

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 TaskScheduler

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 *

"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: 2018-10-31

Share this post


Link to post
Share on other sites
Second

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.

Share this post


Link to post
Share on other sites
Second

 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?

Share this post


Link to post
Share on other sites
jdelaney

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.

Share this post


Link to post
Share on other sites
Second

 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.

Share this post


Link to post
Share on other sites
jdelaney

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.

Share this post


Link to post
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
Sign in to follow this  

  • Similar Content

    • griefman
      By griefman
      Hi everyone,
      i am writing to you after a very long struggle i had while trying to figure out how to send a simple click inside a virtual machine running in vmware workstation 14.
      i have an autoit script running on my host machine watching for the UAC prompt to be displayed in a running vm. Both the host and the guest OS are Windows 10. This script worked perfectly with virtual box. It recognized the UAC prompt and clicked inside and the UAC was accepted. Since i switched to VMware Workstation 14, the script no longer clicks inside the VM successfully. It acts as if it clicks, but it doesn't. 
      I tried sending key combinations instead of a click, so that the VM can grab the input, but it also did not work. Every attempt that i made to send clicks or keys from the host inside the VM did not work. I tried using:
      MouseClick
      ControlClick
      MouseMove
      _WinAPI_Mouse_Event
      _WinAPI_Keybd_Event
       
      I also noticed that while the cursor moves to the target which has to be cilcked when my vmware worstation window is not focused, it even doesn't do that when i WinActivate the vmware workstation window first.
       
      Did anyone experience such an issue, or maybe could give me a hint, what else i could use to send a key combination or a mouse click in a vmware workstation 14 pro guest window?
       
      here is my code, which works with virtualbox:
       
      #AutoIt3Wrapper_Icon=".\uac.ico" #include <ImageSearchSubrogated.au3> FileInstall(".\ImageSearchDLL.dll", ".\ImageSearchDLL.dll", 0) FileInstall(".\UAC_ginloSetup.bmp", ".\UAC_ginloSetup.bmp", 0) FileInstall(".\UAC_Yes.bmp", ".\UAC_Yes.bmp", 0) ; set global variables for the coordinates, which should be delivered global $x1 = 0, $y1 = 0 global $x2 = 0, $y2 = 0 global $counter1 = 0 global $counter2 = 0 global $sleep = 10000 global $smallSleep = 5000 ; execute the script in a loop, so that it will hopefully recover from some unexpected errors While $counter1 < 1 checkForImage() WEnd #cs ------------ Functions #ce ------------ Func checkForImage() While $counter2 < 1 ; search for the UAC in the entire screen - 2 screens supported local $searchUac = _ImageSearchArea('UAC_ginloSetup.bmp', 1, -2568, -8, 5136, 1440, $x1, $y1, 0) If $searchUac = 1 Then ; if the UAC was found search for the Yes button in a an area 200 x 200 from the middle of the found UAC image local $searchYes = _ImageSearchArea('UAC_Yes.bmp', 1, $x1, $y1, $x1 + 200, $y1 + 200, $x2, $y2, 0) If $searchYes = 1 Then ; if the Yes button was found click it and pause the script for $sleep seconds MouseClick("left", $x2, $y2, 1,0) Sleep($sleep) Else ; if the Yes button was not found retry from the beginning in $smallSleep seconds MsgBox(0, "UAC found error", "UAC was found but the 'Yes' button was not found. Script will retry in " & $smallSleep & " seconds.", $smallSleep) EndIf ; another way to accept the UAC - via shortcut ;Send("{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}") ;Send("!y") Else ; if UAC was not found try again in $sleep seconds Sleep($sleep) EndIf WEnd ; if some error occured which expired the loop, pause the script for $sleep seconds MsgBox(0, "Error", "Some Error expired the timer and the script could not recover. The script will restart in " & $sleep & " seconds.", $sleep) EndFunc  
    • davidacrozier
      By davidacrozier
      Hello all ~
      I am running an autoit script on Windows 10 inside VMware Workstation 12 Pro version 12.5.2.  Technically I am remoting into ESXi which has a Domain Controller (DC), WebServer, FilServer, Windows 10, etc.  Using the GUI (i.e. running explorer.exe) I am able to open several different folders successfully.  The desktop, documents, USB external all open without issue.  The network share opening gives me issues.  Whenever I attempt to open \\filserver\users\user\sharedfolder I get the documents folder instead.  I understand that the documents folder is the default for explorer.  I have also attempted to use the letter drive mapped to the network share (Z:) and receive the same result.  When I run this script on Windows 10 alone without  the VM or the ESXi I am able to open the network share without problems.  I have tried to use the net use command to designate a letter M: to the network share folder prior to running the script.  This did not work for me.  
      One additional avenue I think might work is to use the systreeview321 and _GUICtrlTreeView_FindItem to step through the tree looking for the network share.  Once found,  double click on it and see if that opens the shared network folder.  I can click inside the VM with my mouse on the network share and it opens just fine.  Not sure if running up against GUI issues, or permission issues, or what?
      Thanks in advance,
      Davida Crozier
      TestNetworkShare.au3
      This script is a subset of a much larger program, but it illustrates what I am dealing with.

    • CygnusX1
      By CygnusX1
      Hello, 
      I have a dialog box that pops up during an install on a foreign OS. In this case Spanish. The dialog displays a message the program being installed needs to install .Net, CRT, and C++. When inspected the dialog it seems normal. I can get control IDs, Text, Position, everything. But when I try to have AutoIt interact with it e.g. move it, click a button, click on a control, ControlGetFocus, Send("{TAB}"), ControlClick(). Nothing works. What even weirder is I can WinActivate the dialog but then can't move the mouse with MouseMove. If I don't WinActivate(), I can use the MouseMove() function. I can not figure out how to interact with the dialog box.
      Any thoughts on a way to interact with this dialog box?
      Thanks.
       
       
       

    • ur
      By ur
      With the below command I am able to revert a virtual machine in vmware workstation to a snapshot mentioned.
      $sVMPath = <path of the vmx file of virtual machine> $sSnapshot = <snapshot name> $Ret = RunWait(@ComSpec & " /c VMRun.exe -T WS revertToSnapshot ""$sVMPath$"" ""$sSnapshot$"" nogui", "", @SW_HIDE) But the problem here is the virtual machine may contain multiple snapshots with same name because multiple users use same workstation and eventually they are keeping same name sometimes like below or in attachment.
      Both snapshots are with same name and in GUI we can use them with no issue.

       
      But with autoit or with vmrun command line it is showing error as 
      Error: The name does not uniquely identify one snapshot Could anyone please help me on this.
    • VIP
×