Terminating Processes and Stopping Services

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Terminating Processes and Stopping Services

Post by Marcus Tettmar » Thu Mar 24, 2005 1:56 pm

This tip has two functions: KillProcess and StopService. KillProcess takes a process name (e.g. notepad.exe) and StopService takes the display name of the service (as it appears in the control panel services applet). See examples below.

VBSTART
Sub killProcess(pgm)
set wmi = getobject("winmgmts:")
sQuery = "select * from win32_process " & "where name='" & pgm & "'"
set processes = wmi.execquery(sQuery)
for each process in processes
process.terminate
next
End Sub

Sub StopService(sServiceName)
strComputer = "."
Set wbemServices = GetObject("winmgmts:\\" & strComputer)
sWQL = "Select state from Win32_Service " & "Where displayname='" & sServiceName & "'"
Set oResults = wbemServices.ExecQuery(sWQL)
For Each oService In oResults
If Trim(LCase(oService.State)) = LCase("Running") Then
oService.StopService
End If
Next
End Sub

VBEND
VBRun>KillProcess,notepad.exe
VBRun>StopService,Automatic Updates

This is a follow up to this post:
http://www.mjtnet.com/forum/viewtopic.php?t=1652

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts