Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Terminating a process tree

751 views
Skip to first unread message

deadmetal

unread,
Jan 18, 2005, 8:49:08 AM1/18/05
to
Hello There

I'm using the following code to script the termination of a process, but I
was wondering if someone would be kind enough to help me re-write this so
that it will terminate any child processes as well, I would really appreciate
it

Best Regards

Gary
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate,(debug)}!\\" & strComputer &
"\root\cimv2")

Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'calc.exe'")

For Each objProcess in colProcessList
objProcess.Terminate()
Next

Michael Harris (MVP)

unread,
Jan 18, 2005, 10:37:03 AM1/18/05
to
> I'm using the following code to script the termination of a process,
> but I was wondering if someone would be kind enough to help me
> re-write this so that it will terminate any child processes as well,
> I would really appreciate it


Here's what I've used to do this...

' KillProcessTree.vbs
'
' Syntax: KillProcessTree.vbs [/server:servername] /pid:processid
'
'

with wscript.arguments.named
if .exists("server") then
server = .item("server")
else
server = "."
end if
if .exists("pid") then
pid = .item("pid")
'wscript.echo pid, typename(pid), isnumeric(pid)
if not isnumeric(pid) then
wscript.echo "/pid:processID is not numeric"
wscript.quit
elseif cstr(clng(pid)) <> pid then
wscript.echo "/pid:processID is not integer value"
wscript.quit
end if
pid = clng(pid)
else
wscript.echo "/pid:processID is required"
wscript.quit
end if
end with

set wmi = getobject("winmgmts://" & server)

set rootProcess = _
wmi.get("win32_process.handle=" & pid)

KillProcessTree rootProcess

sub KillProcessTree(process)

wscript.echo _
process.name, _
process.handle, _
"is a child of", process.parentprocessid

wql = "select * from win32_process " _
& "where ParentProcessID=" & process.handle

set results = wmi.execquery(wql)

for each childProcess in results
KillProcessTree childProcess
next

process.terminate

end sub


--
Michael Harris
Microsoft.MVP.Scripting

deadmetal

unread,
Jan 19, 2005, 4:47:02 AM1/19/05
to
Michael

Thank you for putting forward your code sample I was able to use it to
complete what I was trying to do, Thank You

sub KillProcessTree(process)
wscript.echo process.name, process.handle, "is a child of",

process.parentprocessid
wql = "select * from win32_process " _
& "where ParentProcessID=" & process.handle

set results = objWMIService.execquery(wql)


for each childProcess in results
KillProcessTree childProcess
next
process.terminate
end sub

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate,(debug)}!\\" & strComputer &
"\root\cimv2")

Set colProcessList = objWMIService.ExecQuery _

("Select * from Win32_Process Where Name LIKE 'ntvdm.%'")

For Each objProcess in colProcessList

set rootProcess = objWMIService.get("win32_process.handle=" &
objProcess.Handle)
KillProcessTree rootProcess
Next

0 new messages