cancel
Showing results for 
Search instead for 
Did you mean: 

Start a service in keyword test

LAB
Contributor

Start a service in keyword test

I've seen the code for jscript and vb, I tried pasting the jscript version into a script and referencing that via "Run Script Routine", entering computer and service name...

 

When testcomplete tries to execute this section, I get the following error.

 

Error
Invalid number of parameters.
Error location:
Unit: "ProjectSuite\Project\Script\Unit2"
Line: 62 Column: 16.
 
which references this section of the script-

 

  wmiService = GetObject("WinMgmts:{impersonationLevel=impersonate}!\\\\" +

     computerName + "\\root\\cimv2");

 

I noticed on the help page it mentions I need to set WMI security permissions, and I did set those permissions as well. Do I need to change anything in this section? Tried changing computerName to my actual computer name but no dice

9 REPLIES 9
Marsha_R
Community Hero

Re: Start a service in keyword test

Let's see your copy of the code please.

LAB
Contributor

Re: Start a service in keyword test

had to split it up due to character limit, but this is what I found on smartbear website

function Test()
{
  StopService(".", "TestComplete 7 Service");
  Delay(10000);
  StartService(".", "TestComplete 7 Service");
}

function StartService(computerName, serviceName)
{
  var service = GetService(computerName, serviceName);
  if (service == null) {
    Log.Warning("The " + serviceName + " service on the " + computerName + " computer was not found");
  }
  else {
    service.StartService();
  }
}
LAB
Contributor

Re: Start a service in keyword test

function StopService(computerName, serviceName)
{
  var service = GetService(computerName, serviceName);
  if (service == null) {
    Log.Warning("The " + serviceName + " service on the " + computerName + " computer was not found");
  }
  else
    service.StopService();
}
LAB
Contributor

Re: Start a service in keyword test

function GetService(computerName, serviceName)
{
  var wmiService, objectsList, eObjectsList, item, sysID;

  wmiService = GetObject("WinMgmts:{impersonationLevel=impersonate}!\\\\" + computerName + "\\root\\cimv2");
  objectsList = wmiService.InstancesOf("Win32_Service");

  if (objectsList.Count > 0) {  
    eObjectsList = new Enumerator(objectsList);
    for (; !eObjectsList.atEnd(); eObjectsList.moveNext()) {
      item = eObjectsList.item();
      if (item.Name == serviceName)
        return item;
    }
  }

  return null;
}
Marsha_R
Community Hero

Re: Start a service in keyword test

Well, yes, I saw it on SmartBear, but I wanted to see exactly what you are trying to run in case there is a typo or something.  😉

LAB
Contributor

Re: Start a service in keyword test

thanks for the response

I think that's my question, I'm just using that code as pasted above, only thing I've tried changing is the "computerName" and "serviceName" sections with my computer name and service name.

 

Is there anything else I'm supposed to change? Do I need to change any part of the wmiService section?

HKosova
Moderator

Re: Start a service in keyword test

Hi LAB,

 

It looks like you are using JavaScript (not JScript), which does not have JScript's GetObject() function. You need to replace GetObject() with

wmiService = Sys.OleObject("WbemScripting.SWbemLocator").ConnectServer( params )

See SWbemLocator.ConnectServer method in MSDN.

 

Also check out this thread:

https://community.smartbear.com/t5/TestComplete-General-Discussions/On-TC12-WMI-or-BulitIn-GetObject...


Helen Kosova
SmartBear Documentation Team Lead
________________________
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
LAB
Contributor

Re: Start a service in keyword test

Tried that change but testcomplete was unable to parse that line of code... checked the link provided and tried changing to this instead

 

function GetService(computerName, serviceName)
{
  var wmiService, objectsList, eObjectsList, item, sysID;
   var locator = Sys.OleObject("WbemScripting.SWbemLocator");
var objSWbemServices = locator.ConnectServer(computerName, "\\root\\cimv2");
 
  objectsList = wmiService.InstancesOf("Win32_Service");
 
  if (objectsList.Count > 0) { 
    eObjectsList = new Enumerator(objectsList);
    for (; !eObjectsList.atEnd(); eObjectsList.moveNext()) {
      item = eObjectsList.item();
      if (item.Name == serviceName)
        return item;
    }
  }
  return null;
}

 

really feels like I'm going down the rabbit hole, error on run is now:

 

---------------------------
TestComplete
---------------------------
JavaScript runtime error.

TypeError
Cannot read property 'InstancesOf' of undefined

Error location:
Unit: "storecallvariables\ValidateErrors\Script\Unit2"
Line: 36 Column: 27.
---------------------------
OK  
---------------------------

HKosova
Moderator

Re: Start a service in keyword test

Your variable names are mixed up:

var objSWbemServices = locator.ConnectServer(computerName, "\\root\\cimv2");
 
objectsList = wmiService.InstancesOf("Win32_Service");   

Helen Kosova
SmartBear Documentation Team Lead
________________________
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
New Here?
Join us and watch the welcome video:
Announcements