-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkillProcess.bat
223 lines (169 loc) · 3.93 KB
/
killProcess.bat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
::
:: Kill a running process by name or pid
::
:: Version: 1.0.1
:: Last change: 17.12.2021
::
@echo off
setlocal enabledelayedexpansion
set prog_name=%~n0
set my_dir="%~dp0"
set "my_dir=%my_dir:~1,-2%"
set /a chk=0
set /a kll=0
set /a pid=0
set /a dlt=0
set target=
set /a verbose=0
set result=
if [%1]==[] goto main
GOTO :ParseParams
:ParseParams
if [%1]==[/?] goto help
if [%1]==[/h] goto help
if [%1]==[/help] goto help
IF /i "%~1"=="/c" (
SET /a chk=1
goto reParseParams
)
IF /i "%~1"=="/d" (
SET /a dlt=1
goto reParseParams
)
IF /i "%~1"=="/k" (
SET /a kll=1
goto reParseParams
)
IF /i "%~1"=="/p" (
SET /a pid=%2
SHIFT
goto reParseParams
)
IF /i "%~1"=="/n" (
SET target=%~2
SHIFT
goto reParseParams
)
IF /i "%~1"=="/v" (
SET /a verbose=1
goto reParseParams
) ELSE (
echo Unknown option : "%~1"
)
:reParseParams
SHIFT
if [%1]==[] goto main
GOTO :ParseParams
:main
if %verbose% == 1 (
echo target : %target%
echo check : %chk%
echo delete : %dlt%
echo kill : %kll%
echo pid : %pid%
)
set /a "c=%chk%+%kll%+%dlt%"
if %c% == 0 (
set /a kll=1
)
if [%pid%]==[] set /a pid=0
if [%target%] == [] (
if %pid% == 0 (
echo [e] No target or pid passed!
echo.
call :usage
exit /B 0
)
)
if %dlt% == 1 set /a kll=0
if %chk% == 1 (
call :checkProcess %target%
)
if %kll% == 1 (
call :killProcess "%target%" %pid%
)
if %dlt% == 1 (
call :deleteProcess "%target%" %pid%
)
endlocal
exit /b 0
:checkProcess
SETLOCAL
set name=%~1
tasklist | find /i "%name%"
ENDLOCAL
exit /b %errorlevel%
:deleteProcess
echo delete %~1 %~2
SETLOCAL
set name=%~1
set pid=%~2
call :deleteProcessEx %name%
ENDLOCAL
exit /b %errorlevel%
:deleteProcessEx
SETLOCAL
set name=%~1
set tp1=%tmp%\deleteSvHostPid.txt
set tp2=%tmp%\deleteSvHostName.txt
wmic process get Name,ProcessID | find /i "%name%" > %tp1%
wmic process get ExecutablePath | find /i "%name%" > %tp2%
set /p pid=<%tp1%
set /p epath=<%tp2%
for %%A in (%pid%) do (
set pid=%%A
)
if [%pid%]==[0] exit /b %errorlevel%
taskkill /f /pid %pid%
del /a "%epath%"
if exist %tp1% del %tp1%
if exist %tp2% del %tp2%
ENDLOCAL
exit /b %errorlevel%
:killProcess
echo kill %~1 %~2
SETLOCAL
set name=%~1
set pid=%~2
if [%pid%]==[0] (
call :killEx %name%
) else (
taskkill /f /pid %pid%
)
ENDLOCAL
exit /b %errorlevel%
:killEx
SETLOCAL
set name=%~1
set tp1=%tmp%\killProcByName.txt
wmic process get Name,ProcessID | find /i "%name%" > %tp1%
for /F "tokens=*" %%A in (%tp1%) do (
call :killLine "%%A"
)
if exist %tp1% del %tp1%
ENDLOCAL
exit /b %errorlevel%
:killLine
SETLOCAL
set pid=%~1
for %%A in (%pid%) do (
set pid=%%A
)
if [%pid%]==[0] exit /b %errorlevel%
taskkill /f /pid %pid%
ENDLOCAL
exit /b %errorlevel%
:usage
echo Usage: %prog_name% [/p ^<pid^>] [/n ^<name^>] [/c] [/k] [/d] [/v] [/h]
exit /B 0
:help
call :usage
echo.
echo Options:
echo /p: Process id.
echo /n: Process name.
echo /c: Check ^<name^> in tasklist to get pid.
echo /k: Kill provided ^<pid^> or ^<name^>.
echo /d: Delete and kill the target ^<name^>.
echo /h: Print this.
exit /B 0