Forum Discussion

bbi's avatar
bbi
Contributor
8 years ago
Solved

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 Get...
  • 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.