AnneTheAgile
10 years agoContributor
jscript quoting error help?
I can't get this to work? It is green no syntax errors but does not kill the exe nor stay open. thank you! function Test() { var oShell = Sys.OleObject("WScript.Shell"); var strCmd ...
- 10 years agoHi Anne,
I think wmic chokes on the parentheses in the WHERE clause. Writing it like where (X and Y or M and Q) should work, because AND has higher priority than OR.
A few notes:
- There is no need to wrap the command into the powershell call - wmic is a standard Windows command.
- I wouldn't recommend attempting to terminate TestComplete from a TestComplete script. :) I changed that to AnotherApp.
var oShell = Sys.OleObject("WScript.Shell");
var strCmd = "wmic process where" +
" (CommandLine like '%Higgens%' and name like '%Higgens%'" +
" or CommandLine like '%AnotherApp%' and name like '%AnotherApp%')" +
" call terminate";
// Run the command and get the exit code
var res = oShell.Run(strCmd, 0 /* run in background */, true /* wait for completion */);
if (res != 0) {
Log.Message("WMIC command failed; exit code " + res);
}
Just for completeness: here's a version that uses WMI scripting objects. It's a bit longer, but may be more convenient to use for handling errors and troubleshooting.
var oWMI = GetObject("winmgmts://./root/cimv2");
var colItems = oWMI.ExecQuery(
"SELECT * FROM Win32_Process WHERE"
+ " CommandLine LIKE '%Higgens%' AND Name LIKE '%Higgens%'"
+ " OR CommandLine LIKE '%AnotherApp%' AND Name LIKE '%AnotherApp%'");
var enumProcesses = new Enumerator(colItems);
var p;
while (!enumProcesses.atEnd()) {
p = enumProcesses.item();
try {
p.Terminate();
}
catch (e) {
Log.Message(
aqString.Format("Failed to terminate the process '%s' with handle %d. Error %d: %s.", p.Name, p.Handle, e.number, e.message),
p.GetObjectText_()
);
}
enumProcesses.moveNext();
}