16

I have this VBScript code to terminate one process

  Const strComputer = "." 
  Dim objWMIService, colProcessList
  Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
  Set colProcessList = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'Process.exe'")
  For Each objProcess in colProcessList 
    objProcess.Terminate() 
  Next  

It works fine with some processes, but when it comes to any process runs under SYSTEM, it can't stop it.

Is there is anything I need to add to kill the process under SYSTEM?

omegastripes
  • 12,351
  • 4
  • 45
  • 96

2 Answers2

18

The way I have gotten this to work in the past is by using PsKill from Microsoft's SysInternals. PsKill can terminate system processes and any processes that are locked.

You need to download the executable and place it in the same directory as the script or add it's path in the WshShell.Exec call. Here's your sample code changed to use PsKill.

Const strComputer = "." 
Set WshShell = CreateObject("WScript.Shell")
Dim objWMIService, colProcessList
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'Process.exe'")
For Each objProcess in colProcessList 
  WshShell.Exec "PSKill " & objProcess.ProcessId 
Next
Jose Basilio
  • 50,714
  • 13
  • 121
  • 117
  • Great work. Thank you very much, I searched for 2 hours on the web with no luck :-), now it works great. –  May 21 '09 at 16:16
1

Try explicit assert debug privilege {impersonationLevel=impersonate,(debug)}:

Set wmi = GetObject("winmgmts:{impersonationLevel=impersonate,(debug)}!\\.\root\CIMV2")
Set procs = wmi.ExecQuery("SELECT * FROM Win32_Process WHERE Name='SearchIndexer.exe'", , 48)
For Each proc In procs
    proc.Terminate
Next
omegastripes
  • 12,351
  • 4
  • 45
  • 96