Forum Discussion
Thank you Robert, let me try.
By the way can i use net stop remotely? I tried entering the path but failed. I need to stop & start the service remotely. Can you help?
- AlexKaras8 years agoCommunity 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
- HKosova8 years ago
Alumni
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", "", ""); - HKosova8 years ago
Alumni
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.
- HKosova8 years ago
Alumni
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; } - CSL8 years agoContributor
Thank you Helen, upgrading to Test Complete ver 12.40 fixed my issue.
- CSL8 years agoContributor
I also need to pass on credentials.
SO how can i use net stop to stop the service remotely with different credentials. Help please!
- tristaanogre8 years agoEsteemed 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.
- CSL8 years agoContributor
thank you trying it.
- CSL8 years agoContributor
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)
- CSL8 years agoContributor
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
- CSL8 years agoContributor
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;
} - HKosova8 years ago
Alumni
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?
- CSL8 years agoContributor
Thank you Helen for your quick reply but it did not work :( item.name did not return any value.
- CSL8 years agoContributor
Yes.