Ask a Question

I need to Start and Stop Window Services using Test complete J script?

SOLVED
VasanthVijay
Contributor

I need to Start and Stop Window Services using Test complete J script?

Hello ,

I need to Start and Stop Window Services using TestComplete J Script?

 

ThanksinAdvances

Vasanth.V

Thanks and Regards,
Vasanth.V
Wittmann Battenfeld India Pvt. Ltd.,
Chennai.
Email : Vasanth.Veeravijayan@wittmann-group.com
Website : www.wittmann-group.com
Mobile: +91 8870219392 | Landline: +91 44 4207700
23 REPLIES 23
HKosova
SmartBear Alumni (Retired)

@CSL that's interesting... Are you connecting using a domain account? Does this account have Admin rights on the remote computer?

 

If you open Command Prompt and run the following commands, do they work ("method execution successful")? It's the command line version of that script example.

wmic /node:COMPUTER_NAME /user:DOMAIN\USER /password:PASSWORD service where name="SERVICE_NAME" call startservice

wmic /node:COMPUTER_NAME /user:DOMAIN\USER /password:PASSWORD service where name="SERVICE_NAME" call stopservice

Service name is what's displayed in the Task Manager > Services > Name column.


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

Yes. 

HKosova
SmartBear Alumni (Retired)

@CSL, try the following code - does it work? Also, try running TestComplete as administrator.

function Test()
{
  let computerName = ".";
  let serviceName = "WSearch"; // Service name is what's displayed in the Task Manager > Services > Name column

  // Use "" if local computer, otherwise use a domain account, e.g. "DOMAIN\\user"
  let userName = "";
  let password = "";

  StartService(computerName, serviceName, userName, password);
  Delay(5000);
  StopService(computerName, serviceName, userName, password);
}

function StartService(computerName, serviceName, userName = "", password = "")
{
  let service = GetService(computerName, serviceName, userName, password);
  if (service == null) {
    Log.Warning(`The '${serviceName}' service was not found on the '${computerName}' computer.`);
  }
  else {
    let status = service.StartService();
    if (status == 0) {
      Log.Message(`Started the '${serviceName}' service on the '${computerName}' computer.`)
    }
    else {
      // For the error codes, see https://msdn.microsoft.com/en-us/library/aa393660(v=vs.85).aspx
      Log.Message(`Could not start the '${serviceName}' service on the '${computerName}' computer. Error ${status}.`);
    }
  }
}
 
function StopService(computerName, serviceName, userName = "", password = "")
{
  let service = GetService(computerName, serviceName, userName, password);
  if (service == null) {
    Log.Warning(`The '${serviceName}' service was not found on the '${computerName}' computer.`);
  }
  else {
    let status = service.StopService();
    if (status == 0) {
      Log.Message(`Stopped the '${serviceName}' service on the '${computerName}' computer.`)
    }
    else {
      // For the error codes, see https://msdn.microsoft.com/en-us/library/aa393673(v=vs.85).aspx
      Log.Message(`Could not stop the '${serviceName}' service on the '${computerName}' computer. Error ${status}.`);
    }
  }
}

function GetService(computerName, serviceName, userName = "", password = "")
{
  let Locator = getActiveXObject("WbemScripting.SWbemLocator");

  // https://msdn.microsoft.com/en-us/library/aa393720(v=vs.85).aspx
  let wmiService = Locator.ConnectServer(computerName, "root\\cimv2", userName, password);

  //objectsList = wmiService.ExecQuery(`SELECT * FROM Win32_Service WHERE Name='${serviceName}'`);
  let service = null;
  try {
    service = wmiService.Get(`Win32_Service.Name='${serviceName}'`);
  }
  catch (e) {
    // Object not found, or missing permissions, or another error.
    // Error code is e.COMExceptionInfo.scode
    // See the error codes in https://msdn.microsoft.com/en-us/library/aa393868(v=vs.85).aspx
    Log.Error(e);
  }

  return service;
}

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

Thank you Helen, upgrading to Test Complete ver 12.40 fixed my issue.

cancel
Showing results for 
Search instead for 
Did you mean: