How to terminate a process if it has specific folder name in its path?

廉价感情. 提交于 2019-12-25 00:20:51

问题


I run an application from PowerBuilder and terminate it when i dont need it. But the application when running, it executes other processes from a specific folder.

I want to kill all processes if they run from a specific folder.

How to get the process handles of all those processes that have specif folder name in their path?


回答1:


Here an example to show how to terminate all running instance of Notepad.exe

OleObject wsh
integer  li_rc

wsh = CREATE OleObject
wsh.ConnectToNewObject( "MSScriptControl.ScriptControl" )
wsh.language = "vbscript"
wsh.AddCode('function terminatenotepad() ~n ' + &
 'strComputer = "." ~n ' + &
 'Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") ~n ' + &
 'Set colItems = objWMIService.ExecQuery("Select * from Win32_Process where name = ~'notepad.exe~'") ~n ' + &
 'For Each objItem in colItems ~n ' + &
 '     objItem.Terminate ~n ' + &
 'Next ~n ' + &
 'end function ~n ' )
wsh.executestatement('terminatenotepad()')
wsh.DisconnectObject()
DESTROY wsh

Try to change the where clause

where name = ~'notepad.exe~'" ...

for

where commandline = ~'" + ls_commandline + "~'"  ...

where ls_commandline is the command line used to start your process.

See "Win32_Process class" for more infos.




回答2:


/// This is simple code example Terminate a Running Process if here is specific
/// folder name in the path of the running program (\EagleGet\)
/// Two variable SelectThis and TheWhere can be changed to get different 
/// results. Many things are not  dynamic in this script and certainly not the
/// best way of writing code. but its just ok for a little quick work.`

OleObject wsh
Integer  li_rc

String LineFeed = ' ~r~n ' 
String TheComputer = "." //local computer
String TheCode = "no need to set it here"
String FunctionName = "Whatever()" //any name you like
String SelectThis = "?" //only the columns/expressions
String TheWhere = "?" //only the where clause without keyword WHERE
String DoWhat = "?" //the action to perform for example 'Terminate' without quotes
String TheQuery = "no need to set here"
String WMIClass = "Win32_Process" /// The WMI class of running processes
String TheFolderName = "The folder name from which the creapy process is running (path) " 

/// You just set DoWhat, SelectThis and TheWhere. Rest of the variables, you dont need to set here
/// SelectThis = is the columns or expressions you want returned by the Query
SelectThis = "*"

/// TheFolderName = set it to the name of the folder that exist in the path 
///of the ruuning process you want to terminate
TheFolderName = "EagleGet"                                      

/// TheWhere is the WHERE clause expressions
TheWhere = "ExecutablePath  LIKE ~'%\\" + TheFolderName + "\\%~' "

/// DoWhat is the final function call of the WMI Class
DoWhat = "Terminate"

/// There is no need to chage anything from this point onward.
/// without double quotes, and you dont have to change TheQuery here 
TheQuery = " SELECT " + SelectThis + " FROM " + WMIClass + " WHERE " + TheWhere

TheCode = "Function " + FunctionName + LineFeed + &
"strComputer = ~"" + TheComputer  + "~"" + LineFeed + &
"Set objWMIService = GetObject(~"winmgmts:\\~" & strComputer & ~"\root\cimv2~")" + LineFeed + &
"Set colItems = objWMIService.ExecQuery(~"" + Trim(TheQuery) + "~" )" + LineFeed + &
   "For Each objItem in colItems" + LineFeed + &
      "objItem." + Trim(DoWhat) + LineFeed + &
   "Next " + LineFeed + &
"END Function " + LineFeed 

wsh = CREATE OleObject
wsh.ConnectToNewObject("MSScriptControl.ScriptControl")
wsh.Language = "VBScript"
wsh.AddCode(TheCode)
TRY
    wsh.ExecuteStatement(FunctionName)
CATCH (RunTimeError Re01)
    MessageBox("Query Error", "Following code has some problems.~r~n~r~n" + TheCode, StopSign!)
END TRY
wsh.DisconnectObject()

DESTROY wsh


来源:https://stackoverflow.com/questions/49880237/how-to-terminate-a-process-if-it-has-specific-folder-name-in-its-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!