Forum Discussion

AnneTheAgile's avatar
AnneTheAgile
Contributor
10 years ago
Solved

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 = "powershell -command -noexit " + "wmic process where "+ "(CommandLine like '%Higgens%' and name like '%Higgens%') or (CommandLine like '%TestComplete%' and name like '%TestComplete%' " ;

var oExec = oShell.Exec( strCmd + "" )



// ok; var oExec = oShell.Exec("powershell -command Get-Process");



// red; std no escape

// var oExec = oShell.Exec("powershell -command 'wmic process where "(CommandLine like \'%Higgens%\' and name like \'%Higgens%\') or (CommandLine like \'%TestComplete%\' and name like \'%TestComplete%\')" call terminate



// red; \'

// var oExec = oShell.Exec("powershell -command 'wmic process where "(CommandLine like \'%Higgens%\' and name like \'%Higgens%\') or (CommandLine like \'%TestComplete%\' and name like \'%TestComplete%\')" call terminate )



//grn but not kill

// var strCmd = "powershell -command -noexit " + "wmic process where "+ "(CommandLine like '%Higgens%' and name like '%Higgens%') or (CommandLine like '%TestComplete%' and name like '%TestComplete%' " ;



//grn but not working

// var strCmd = "powershell -command -noexit " + "wmic process where "+ "(CommandLine like '%Higgens%' and name like '%Higgens%') or (CommandLine like '%TestComplete%' and name like '%TestComplete%' " ;



}

  • Hi 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();

    }

2 Replies

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Hi 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();

    }

  • thank you! I wrapped your powershell call in a function Test {} and it works great. Yes, indeed I think "it may be more convenient to use for handling errors and troubleshooting."