Forum Discussion

rminnich's avatar
rminnich
Contributor
12 years ago

WMI help

I am trying to follow the example for using the WMI plug in included in TestComplete 8.5.  I am having no success using the plug in.



I have peiced togather this subrouting from the Testcomplete help.  At this point I am trying to populate a collection with valid values.




sub t


Dim i


WMI.ComputerName="."


  Dim OSColl, OSItem


  Set OSColl = WMI.Service.ExecQuery("SELECT * FROM Win32_OperatingSystem")


    i=0


  For Each OSItem In OSColl


    i=i+1


  Next


end sub



When I place a break point on the for each loop and then step to the next statement,

I get an error stating that the object is not a collection.  I have run this both as my normal user and as administrator.



My end goal is to retrieve the suitemask using the WMI plugin.  I am trying to avoid using powershell (that requires an additional software package to be installed for each setup) and using a custom .Net function via the CLR bridge (I already do that once and I know it works.  I would like to replace it in the future if I could get the WMI plug in to work).

  • Hi Russel,



    I tried your code and got the same error.

    To say the truth, I don't know its reason, but on the other hand, I'm not really interested, because WMI plugin always lacked the functionality I needed (sorry, SmartBear :) ) and I prefered to use 'pure' WMI code. The major (for me) thing that WMI plugin does not provide is that it cannot connect to remote box using different credentials.



    Simple and fast solution to your problem:

    replace

    WMI.Service.ExecQuery(...)

    with

    strComputerName = "."

    Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputerName & "\root\cimv2")

    oWMI.ExecQuery(...)



    More functional approach:

    Consider the following function (it is on DelphiScript but I believe that it will not be a problem to move it to VBScript):


    //-----------------------------------------------------------------------------



    function GetWMIService(strServerName : string = '.'; strNamespace : string = 'root\cimv2'; strUser : string = 'domain\user', strPwd : string = '') : OleVariant;

      const cProcName = 'GetWMIService';

      const cProcNameMsgPrefix = cUnitNameMsgPrefix + cProcName + '(): ';



      var objSWbemLocator : OleVariant;

    begin

      try

        objSWbemLocator := Sys.OleObject('WbemScripting.SWbemLocator');

        if ((0 = aqString.Compare(Sys.HostName, strServerName, false)) or ('.' = strServerName)) then

          // Get instance of WMI service on the local computer

          result := objSWbemLocator.ConnectServer('.', strNamespace)

        else

          // Connect to target machine via WMI

          result := objSWbemLocator.ConnectServer(strServerName, strNamespace,

                  strUser, strPwd, '', '', $80);

      except

        Log.Error(cProcNameMsgPrefix + 'Error getting WMI service', ExceptionMessage);

      end;

    end;

    //-----------------------------------------------------------------------------



    Call sample (and alternative to For Each iterator):

    //-----------------------------------------------------------------------------



    function GetTZOffset(strComputerName : string = '.') : integer;

      var objWMIService : OleVariant;

      var colOperatingSystems : OleVariant;

      var objOperatingSystem : OleVariant;

      var i : integer;

    begin



      objWMIService := GetWMIService(strComputerName, 'root\cimv2');

      colOperatingSystems := objWMIService.ExecQuery('Select * from Win32_OperatingSystem');



      i := 0;

      while i < colOperatingSystems.Count do

      begin

        //Number, in minutes, an operating system is offset from Greenwich mean time (GMT). The number is positive, negative, or zero.

        result := colOperatingSystems.ItemIndex(i).CurrentTimeZone;

        i := i + 1;

      end;

    end;

    //----------------------------------------------------------------------------- 


  • AlexKaras's avatar
    AlexKaras
    Champion Level 3
    Hi Russel,



    I tried your code and got the same error.

    To say the truth, I don't know its reason, but on the other hand, I'm not really interested, because WMI plugin always lacked the functionality I needed (sorry, SmartBear :) ) and I prefered to use 'pure' WMI code. The major (for me) thing that WMI plugin does not provide is that it cannot connect to remote box using different credentials.



    Simple and fast solution to your problem:

    replace

    WMI.Service.ExecQuery(...)

    with

    strComputerName = "."

    Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputerName & "\root\cimv2")

    oWMI.ExecQuery(...)



    More functional approach:

    Consider the following function (it is on DelphiScript but I believe that it will not be a problem to move it to VBScript):


    //-----------------------------------------------------------------------------



    function GetWMIService(strServerName : string = '.'; strNamespace : string = 'root\cimv2'; strUser : string = 'domain\user', strPwd : string = '') : OleVariant;

      const cProcName = 'GetWMIService';

      const cProcNameMsgPrefix = cUnitNameMsgPrefix + cProcName + '(): ';



      var objSWbemLocator : OleVariant;

    begin

      try

        objSWbemLocator := Sys.OleObject('WbemScripting.SWbemLocator');

        if ((0 = aqString.Compare(Sys.HostName, strServerName, false)) or ('.' = strServerName)) then

          // Get instance of WMI service on the local computer

          result := objSWbemLocator.ConnectServer('.', strNamespace)

        else

          // Connect to target machine via WMI

          result := objSWbemLocator.ConnectServer(strServerName, strNamespace,

                  strUser, strPwd, '', '', $80);

      except

        Log.Error(cProcNameMsgPrefix + 'Error getting WMI service', ExceptionMessage);

      end;

    end;

    //-----------------------------------------------------------------------------



    Call sample (and alternative to For Each iterator):

    //-----------------------------------------------------------------------------



    function GetTZOffset(strComputerName : string = '.') : integer;

      var objWMIService : OleVariant;

      var colOperatingSystems : OleVariant;

      var objOperatingSystem : OleVariant;

      var i : integer;

    begin



      objWMIService := GetWMIService(strComputerName, 'root\cimv2');

      colOperatingSystems := objWMIService.ExecQuery('Select * from Win32_OperatingSystem');



      i := 0;

      while i < colOperatingSystems.Count do

      begin

        //Number, in minutes, an operating system is offset from Greenwich mean time (GMT). The number is positive, negative, or zero.

        result := colOperatingSystems.ItemIndex(i).CurrentTimeZone;

        i := i + 1;

      end;

    end;

    //-----------------------------------------------------------------------------