RE: Terminating a Process Tree

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: florence (florence_at_discussions.microsoft.com)
Date: 01/18/05


Date: Tue, 18 Jan 2005 06:43:05 -0800

Hi,
This should to it, though I haven't tested :

 Set colProcessList = objWMIService.ExecQuery _
            ("Select * from Win32_Process Where Name = 'calc.exe'")
 For Each objProcess in colProcessList
        set colChildrenList = objWMIService.ExecQuery _
            ("Select * from Win32_Process Where ParentProcessID = " &
objProcess.ProcessID)
         For Each objChild in colChildrenList
              objChild.Terminate()
         next
         objProcess.Terminate()

 Next

Flo

"deadmetal" wrote:

> 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
>