Forum Discussion

chriswal's avatar
chriswal
Occasional Contributor
14 years ago

access VMWare hosts vis VIX API

Hello,

When i try to access an VMWare guest via the Vix API i get always the error message object required.

When i look to the VMWare samples it is necessary to start the script with the .wsf file.

But this is not possible in Testcomplete. So how can i access any guest via VIX in Testcomplete?



Regards

Christian

9 Replies


  • Hello Christian,





    As far as I know, it is not required to use the WSF file to accomplish the task.

    All you need is to create an instance of the "VixCOM.VixLib" COM object in your script, and you will have access to its methods and properties.

    The WSF file contains constants you may need to work with the object, so, you will have to get the values of the constants from the WSF file by creating a simple script outside of TestComplete - the script will get the values of the constants and show them in a message box. Then, you can use the values of the constants in your TestComplete script.
  • chriswal's avatar
    chriswal
    Occasional Contributor
    Thats the code thats in the wsf file

    there are no constants in.




    <job id="powerOn">

       <reference object="VixCOM.VixLib" />

       <script language="VBScript" src="powerOn1.vbs" />

    </job>


    But i made msgboxes ad get the constants.

    But now i have the next error in this script. (thats an example you wrote anywhere in the forum)

    i got the error on line 15 ( Set host = results(0) ) types not matching results

     

    sub main

    Dim lib

    Dim host

    Dim vm

    Dim err

    Dim results

    Dim job









     

    Set lib = CreateObject("VixCOM.VixLib") ' instantiate a COM object









    ' use obtained COM object

    Set job = lib.Connect(1, 3, 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





    err = job.Wait(Array(3010), results)

    If lib.ErrorIndicatesFailure(err) Then

       ' Handle the error...

    End If





    Set host = results(0)





    Set job = host.OpenVM("d:\virtual machines\windows server 2003 enterprise edition\windows server 2003 enterprise edition.vmx", Nothing)

    err = job.Wait(Array(3010), results)

    If lib.ErrorIndicatesFailure(err) Then

       ' Handle the error...

    End If





    Set vm = results(0)





    Set job = vm.PowerOn(512, Nothing, Nothing)

    err = job.WaitWithoutResults()

    If lib.ErrorIndicatesFailure(err) Then

       ' Handle the error...

    End If

    end sub

  • Hello Christian,





    It looks like the code you are using is identical to that given in the VIX API Reference:

    http://pubs.vmware.com/vix-api/ReferenceGuide/lang/com/functions/PowerOn.html





    I don't think TestComplete affects the way the script works - I think there is a problem in the script.

    To check whether the script itself works, put it to a VBS file (for example, Test.vbs) and run the script by double-clicking the file in Windows Explorer.





    If your script works when put in a VBS file, but does not work when executed from TestComplete (without any modifications), let me know.
  • chriswal's avatar
    chriswal
    Occasional Contributor
    i am running 7.50.663.7 on windows 7 ultimate 64bit

    the example works when i run the wsf file with the cscript from syswow64.

    I will update testcomplete today and check if there is any difference.


  • chriswal's avatar
    chriswal
    Occasional Contributor
    i tried the script running as vbs and got also this error .

    i found that this error was a result of an unhandled error.

    i put the error handling code from the vix example to this script, and get the error "the specified version was not found."

    so it seems to be a problem with the vix api. 

    when i try to start with vmrun i get the same error.



    thanks for your help
  • chriswal's avatar
    chriswal
    Occasional Contributor
    the problem with vmrun was that the version of my VMWare Workstation was not in the vixwrapper-config.txt included so i had to add it




    # Workstation 7.0.0

    ws 9 vmdb 7.0.0 Workstation-7.0.0

    ws 9 vmdb 7.0.1 Workstation-7.0.0

    ws 9 vmdb e.x.p Workstation-7.0.0

    player   9  vmdb  3.0.0 Workstation-7.0.0

    player   9  vmdb  e.x.p Workstation-7.0.0




    after that vmrun works



    when i try to run the script with the wsf it works (the constants taken from the vix api)

    when i try to run the script with the extracted constants i get  an runtime error invalid procedure call or invalid argument



    by the way api_version should be -1 in the script




    Dim lib

    Dim host

    Dim vm

    Dim err

    Dim results

    Dim job

    Const JOB_RESULT_HANDLE =3010

    Const VMPOWEROP_LAUNCH_GUI=512

    Const API_VERSION=-1

    Const SERVICEPROVIDER_VMWARE_WORKSTATION=3





    Set lib = CreateObject("VixCOM.VixLib") ' instantiate a COM object









    ' use obtained COM object

    Set job = lib.Connect(API_VERSION, 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





    err = job.Wait(Array(JOB_RESULT_HANDLE), results)

    If lib.ErrorIndicatesFailure(err) Then

       QuitIfError(err)

    End If





    Set host = results(0)





    Set job = host.OpenVM("d:\virtual machines\windows server 2003 enterprise edition\windows server 2003 enterprise edition.vmx", Nothing)

    err = job.Wait(Array(JOB_RESULT_HANDLE), results)

    If lib.ErrorIndicatesFailure(err) Then

          QuitIfError(err)

    End If





    Set vm = results(0)





    Set job = vm.PowerOn(VMPOWEROP_LAUNCH_GUI, Nothing, Nothing)

    err = job.WaitWithoutResults()

    If lib.ErrorIndicatesFailure(err) Then

          QuitIfError(err)

    End If





    '--------------------------------------------------------------------------------

    ' 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
  • chriswal's avatar
    chriswal
    Occasional Contributor
    i modified the code so that i do not have to change too much for calling with the wsf file.



    this is the wsf file content:



    <job id="test-wowsf2">


       <reference object="VixCOM.VixLib" />

       <script language="VBScript" src="test-wowsf2.vbs" />

    </job>



    this is part of the script that you can see the difference between this and the code at the end




    Dim lib


    Dim host

    Dim vm

    Dim err

    Dim results

    Dim job





    dim JOB_RESULT_HANDLE 

    dim VMPOWEROP_LAUNCH_GUI

    dim API_VERSION

    dim SERVICEPROVIDER_VMWARE_WORKSTATION





    JOB_RESULT_HANDLE =VixCOM.Constants.VIX_PROPERTY_JOB_RESULT_HANDLE

    VMPOWEROP_LAUNCH_GUI=VixCOM.Constants.VIX_VMPOWEROP_LAUNCH_GUI

    API_VERSION=VixCOM.Constants.VIX_API_VERSION

    SERVICEPROVIDER_VMWARE_WORKSTATION=VixCOM.Constants.VIX_SERVICEPROVIDER_VMWARE_WORKSTATION

    WScript.Echo("ApiVersion " & API_VERSION &" JobResultHandle " & JOB_RESULT_HANDLE &" ServiceProviderVMWareWorkstation " & SERVICEPROVIDER_VMWARE_WORKSTATION & " VMPowerOPLaunchGui "& VMPOWEROP_LAUNCH_GUI)

    Set lib = CreateObject("VixCOM.VixLib") ' instantiate a COM object





    This is the ouput of the console from the working call :

    C:\Program Files (x86)\VMware\VMware VIX>c:\windows\SysWOW64\cscript "C:\Program

     Files (x86)\VMware\VMware VIX\Samples\test-wowsf2.wsf"

    Microsoft (R) Windows Script Host, Version 5.8

    Copyright (C) Microsoft Corporation 1996-2001. Alle Rechte vorbehalten.





    ApiVersion -1 JobResultHandle 3010 ServiceProviderVMWareWorkstation 3 VMPowerOPL

    aunchGui 512




    The code below called as vbs results in:





    C:\Program Files (x86)\VMware\VMware VIX>c:\windows\SysWOW64\cscript "C:\Program

     Files (x86)\VMware\VMware VIX\Samples\test-wowsf3.vbs"

    Microsoft (R) Windows Script Host, Version 5.8

    Copyright (C) Microsoft Corporation 1996-2001. Alle Rechte vorbehalten.





    ApiVersion -1 JobResultHandle 3010 ServiceProviderVMWareWorkstation 3 VMPowerOPL

    aunchGui 512

    C:\Program Files (x86)\VMware\VMware VIX\Samples\test-wowsf3.vbs(27, 1) Laufzeit

    fehler in Microsoft VBScript&colon; Ungültiger Prozeduraufruf oder ungültiges Argument


    (sorry i have a german windows ;-) invalid procedure call or invalid argument)



    Dim lib

    Dim host

    Dim vm

    Dim err

    Dim results

    Dim job





    dim JOB_RESULT_HANDLE 

    dim VMPOWEROP_LAUNCH_GUI

    dim API_VERSION

    dim SERVICEPROVIDER_VMWARE_WORKSTATION





    JOB_RESULT_HANDLE =3010

    VMPOWEROP_LAUNCH_GUI=512

    API_VERSION=-1

    SERVICEPROVIDER_VMWARE_WORKSTATION=3

    WScript.Echo("ApiVersion " & API_VERSION &" JobResultHandle " & JOB_RESULT_HANDLE &" ServiceProviderVMWareWorkstation " & SERVICEPROVIDER_VMWARE_WORKSTATION & " VMPowerOPLaunchGui "& VMPOWEROP_LAUNCH_GUI)

    Set lib = CreateObject("VixCOM.VixLib") ' instantiate a COM object









    ' use obtained COM object

    Set job = lib.Connect(API_VERSION, 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





    err = job.Wait(Array(JOB_RESULT_HANDLE), results)

    If lib.ErrorIndicatesFailure(err) Then

       QuitIfError(err)

    End If





    Set host = results(0)





    Set job = host.OpenVM("d:\virtual machines\windows server 2003 enterprise edition\windows server 2003 enterprise edition.vmx", Nothing)

    err = job.Wait(Array(JOB_RESULT_HANDLE), results)

    If lib.ErrorIndicatesFailure(err) Then

          QuitIfError(err)

    End If





    Set vm = results(0)





    Set job = vm.PowerOn(VMPOWEROP_LAUNCH_GUI, Nothing, Nothing)

    err = job.WaitWithoutResults()

    If lib.ErrorIndicatesFailure(err) Then

          QuitIfError(err)

    End If





    '--------------------------------------------------------------------------------

    ' 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
  • chriswal's avatar
    chriswal
    Occasional Contributor
    I found the solution:

    The problem is, that the datatypes not the same if you are using your own variables

    The datatype from the constants is LONG so you have to convert your variable.

    when you run it from wsf file with the vix constants all subtypes of the variables are LONG but in this sample it is only necessary to convert the JOB_RESULT_HANDLE to avoid the error.




    JOB_RESULT_HANDLE =3010

    VMPOWEROP_LAUNCH_GUI=512

    API_VERSION=-1

    SERVICEPROVIDER_VMWARE_WORKSTATION=3

    JOB_RESULT_HANDLE=clng(JOB_RESULT_HANDLE)





    WScript.Echo("ApiVersion " & API_VERSION &" JobResultHandle " & JOB_RESULT_HANDLE &" ServiceProviderVMWareWorkstation " & SERVICEPROVIDER_VMWARE_WORKSTATION & " VMPowerOPLaunchGui "& VMPOWEROP_LAUNCH_GUI)

    WScript.Echo("ApiVersion " & vartype(API_VERSION) &" JobResultHandle " & vartype(JOB_RESULT_HANDLE) &" ServiceProviderVMWareWorkstation " & vartype(SERVICEPROVIDER_VMWARE_WORKSTATION) & " VMPowerOPLaunchGui "& vartype(VMPOWEROP_LAUNCH_GUI))



  • Hello Christian,





    Great to hear you have solved the problem. Thanks for sharing the solution!