cancel
Showing results for 
Search instead for 
Did you mean: 

Start a service in keyword test

Highlighted
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
Highlighted
Community Hero

Let's see your copy of the code please.

Highlighted
Contributor

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();
  }
}
Highlighted
Contributor

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();
}
Highlighted
Contributor

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;
}
Highlighted
Community Hero

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.  😉

Highlighted
Contributor

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?

Highlighted
Moderator

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
________________________
Learn SmartBear products in a fun and easy way and prove your knowledge!
>>Participate in the TechCorner Challenge today
Highlighted
Contributor

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  
---------------------------

Highlighted
Moderator

Your variable names are mixed up:

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

Helen Kosova
SmartBear Documentation Team Lead
________________________
Learn SmartBear products in a fun and easy way and prove your knowledge!
>>Participate in the TechCorner Challenge today
New Here?
Join us and watch the welcome video:
Announcements
Join TechCorner Challenge!
Want a fun and easy way to learn TestComplete? Try solving weekly TechCorner challenges and get into the Leaderboard!


Challenge Status

Get properties of a web page element

See replies!

Compare images using the Region Checkpoint

See replies!

Compare HTML table with Excel file and correct data in Excel file

Participate!

How to execute remote test and obtain results via Test Runner REST API

Participate!
Top Kudoed Authors