Forum Discussion

rminnich's avatar
rminnich
Contributor
12 years ago
Solved

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...
  • AlexKaras's avatar
    12 years ago
    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;

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