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
tristaanogre
Esteemed Contributor

In that case, I'd suggest looking into the SC (service controller) command for this process.  You should run it with elevated privileges.

 

As for passing in credentials, not sure how to go about that off the top of my head but I found the SC command prompt stuff (https://community.spiceworks.com/how_to/5372-remotely-start-or-stop-service-from-cmd) with a quick Google search.  There should be some way of doing that as well.  The key is how to run the command prompt via TestComplete... and WshShell is the way to do it.


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
m_essaid
Valued Contributor

hi,

I just scripted that a few days ago :

 

procedure xxxx

var
  wmiService, service;
begin
  wmiService:= WMI.Service;
  service:= wmiService.Get('Win32_Service.Name=''theServiceName''');
  service.StartService

 

please note that "theServiceName" is to be changed by the service name as it appears in windows services

 

also, "service.StopService" stops the service

 

finally, both return an integer. 0 indicates that the operation has correctly been processed.

 

see this page for all return codes :

 

https://msdn.microsoft.com/en-us/library/aa393660(v=vs.85).aspx

thank you trying it.

CSL
Contributor

I found a dos command that can stop & start the service. However it doesn't do anythign when i run it from Testcomplete

 

belwo is what I did, but it didn't do anything.

 

comExecute = "psservice \\\\" + serverName + " -u " + userName + " -p " + pWord + " " + command + " " + String.fromCharCode(34) + txtService + String.fromCharCode(34)

 

Sys.OleObject("WScript.Shell").Exec("cmd /c " + comExecute)

 

 

 

AlexKaras
Community Hero

Hi,

 

> I found a dos command

psservice is not a dos command but a utility by Sysinternals that can be downloaded from Microsoft site.

 

If you need to pass credentials to control remote machine, I would recommend WMI (like in the example from @m_essaid) with additional SWbemLocator object:

https://stackoverflow.com/questions/3929873/pass-credentials-to-wmi-call-in-vbscript

https://msdn.microsoft.com/en-us/library/aa389290(v=vs.85).aspx

 

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================

Hi ALex,

 

Thank you. But I am using javascript, I found the sample jscript in TestComplete website but they don't work in javascript. That's why I switched to using the psservice approach. I don't mind which approach as long as it works in javascript. This issue is holding me up for weeks now 😞 I hope you can assist.

 

I need to start the service remotely with credentials using javascript.

 

I appreciate your help.

 

Thank you so much,

 

 

I tried adding - SWbemLocator.ConnectServer  but failed as well. Can you

 

 

HKosova
SmartBear Alumni (Retired)

Hi CSL,

 

You can use this example, just replace this line

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

with the JavaScript version:

  let Locator = getActiveXObject("WbemScripting.SWbemLocator");

  // the params are: computer name, namespace, username, password
  wmiService = Locator.ConnectServer(".", "\\root\\cimv2", "", "");

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

Helen,

 

Thank you for your help, I replaced my code with your suggestion and i bypassed the error but !eObjectsList.atEnd() always return true though objectsList.Count returns 150. Thus my script still failed. It cannot find the service that I sent.

 

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

let Locator = Sys.OleObject("WbemScripting.SWbemLocator");

wmiService = Locator.ConnectServer(computerName, "\\root\\cimv2", "domain\\testUser", "testPass");
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;
}

HKosova
SmartBear Alumni (Retired)

Hi CSL,

 

Try replacing

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

with

  if (objectsList.Count > 0) {
    for (let i = 0; i < objectsList.Count; i++) {
      item = objectsList.ItemIndex(i);
      if (item.Name == serviceName)
        return item;
    }
  }

Does this work?


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 for your quick reply but it did not work 😞 item.name did not return any value.

cancel
Showing results for 
Search instead for 
Did you mean: