Forum Discussion

bbi's avatar
bbi
Contributor
8 years ago

On TC12 WMI or BulitIn GetObject does not work anymore (working in TC11)

Hello everybody,


The following code worked well untill we upgrade to TC 12.

It is not only this specific piece of code, it's all calls to GetObject which fails.
Whatever kind of call of GetObject we make, we have the error :

Error: Invalid number of parameters.

at Error (native)

Of course TC is launched with administrator rights.
Operating system is W10 Pro.


qa.services.getService = function (ServiceName, Host) { Host = CheckHost(Host); ServiceName = qa.system.checkUndefined(ServiceName, ''); var result = null; if (ServiceName != '') { try { var objSWbemServices = GetObject("winMgmts:{impersonationLevel=impersonate}!" + Host + "\\root\\cimv2"); result = objSWbemServices ? objSWbemServices.Get("Win32_Service.Name='" + ServiceName + "'") : null; } } catch(e) { qa.system.logExceptionByLevel(e, "getService(" + ServiceName + ", " + Host + ")"); } } return result; };


Any help would be greatly appriecated !


 

  • bbi's avatar
    bbi
    8 years ago

    Ok, got it !

     

    So, the correct way is to always give the username and password parameters on ConnectServer but keep them to empty string if you connect to localhost.

     

    Here the full code of the working method:

     

      /**
       * <a id="services.getService"></a>
       * Récupérer un objet de type Service
       * @function
       * @param {string} ServiceName - Nom du service à rechercher
       * @param {string} [Host=Sys.HostName] - Nom de la machine hôte sur laquelle chercher le service, par défaut c'est sur la machine courante
       * @param {string} [UserName=''] - Nom d'utilisateur à utiliser pour la connexion a l'objet WMI, ne doit être valorisé que pour une machine distante
       * @param {string} [Password=''] - Mot de passe à utiliser pour la connexion a l'objet WMI, ne doit être valorisé que pour une machine distante
       * @returns {object} Renvoie l'objet Service s'il a été trouvé ou <b>null</b> si aucune service correspondant trouvé. En cas d'erreur un log d'erreur est inscrit
       */
      qa.services.getService = function (ServiceName, Host, UserName, Password) {
        ServiceName = qa.system.checkUndefined(ServiceName, '');
        var result  = null;
        if (ServiceName != '') { 
          Host     = CheckHost(Host);
          if (Host == '\\\\.') {
            UserName = '';
            Password = '';
    Host = '.'; } else { UserName = qa.system.checkUndefined(UserName, ''); Password = qa.system.checkUndefined(Password, ''); } try { var locator = Sys.OleObject("WbemScripting.SWbemLocator"); var objSWbemServices = locator.ConnectServer(Host, "\\root\\cimv2", UserName, Password); result = objSWbemServices ? objSWbemServices.Get("Win32_Service.Name='" + ServiceName + "'") : null; } catch(e) { qa.system.logExceptionByLevel(e, "getService(" + ServiceName + ", " + Host + ")"); } } return result; };

     

     

    Txs for helping me.

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    HI bbi,

     

    Did you upgrade your projects from JScript to JavaScript? GetObject function does not exist in JavaScript. You need to change this line:

    var objSWbemServices = GetObject("winMgmts:{impersonationLevel=impersonate}!" + Host + "\\root\\cimv2");

    to:

    var locator = Sys.OleObject("WbemScripting.SWbemLocator");
    var objSWbemServices = locator.ConnectServer(Host, "\\root\\cimv2");

    This code will work in both JavaScript and JScript.

    • bbi's avatar
      bbi
      Contributor

      Tried it but an error still occurs:

       

        Error: SWbemLocator
        Invalid parameter
         at Error (native)

       

      It occurs on 

       

      var locator = Sys.OleObject("WbemScripting.SWbemLocator");
      • bbi's avatar
        bbi
        Contributor

        In addition the initial code works well with TC 12 and Windows 7

         

        The problem seems to be only with Windows 10 pro  (we tried on 2 VMs in Windows 10).