Forum Discussion
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,
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
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.
- 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.