Question Locked

Killing a program/process with VBscript

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I'm trying to run/kill a defrag program/process after it runs but I'm not sure how to go about it with VBscript. Here's what I've got:
'Program to run Defraggler (Hard drive defragger)

strComputer = "."

Dim WshShell

Set WshShell = CreateObject("wscript.Shell")

'Open Defraggler
WshShell.Run("""C:\Program Files\Defraggler\Defraggler.exe""")

'Sleep for 2 seconds
WScript.Sleep 2000

'Wait until Defraggler has loaded
While WshShell.AppActivate("Piriform Defraggler") = False
WScript.Sleep 100
Wend

'Bring Defraggler to the foreground
WshShell.AppActivate("Piriform Defraggler")

'Send Alt-A keystroke
WshShell.SendKeys "%A"
WScript.Sleep 1000

WshShell.SendKeys "{DOWN}"
WScript.Sleep 1000

'Send ENTER keystroke
WshShell.SendKeys "{ENTER}"
WScript.Sleep 1000

'Send ENTER keystroke
WshShell.SendKeys "{ENTER}"

WScript.Sleep 1000

'Ensures Virtual Memory is given time to increase
Wscript.Sleep 100000

'Select an instance of Win32_process = Defraggler
Set wmi = getobject("winmgmts:")
sQuery = "select * from win32_process " & "where name='Defraggler.exe'"
set processes = wmi.execquery(sQuery)

'Allow Defraggler to run while Virtual memory size is greater than 14000
Do while WorkingSetSize > 14000
wscript.sleep 500
set processes = wmi.execquery(sQuery)
Loop

'Exit the program now that the Defraggler is finished updating
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & _
"\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'Defraggler.exe'")
For Each objProcess in colProcessList
objProcess.Terminate()
Next

It starts the defrag program, but it doesn't kill the program after it runs. All advice is wonderful.

3 replies

by robo_dev

It might be better to declare the process as a variable, but that's your call

The fault is in your query:

Yours:

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

The quote should be after the = sign:

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

See this example:
ProcessKillLocal.vbs
' Sample VBScript to kill a program
' Author Guy Thomas http://computerperformance.co.uk/
' Version 2.7 - December 2005
' ------------------------ -------------------------------'
Option Explicit
Dim objWMIService, objProcess, colProcess
Dim strComputer, strProcessKill
strComputer = "."
strProcessKill = "'calc.exe'"

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

Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & strProcessKill )
For Each objProcess in colProcess
objProcess.Terminate()
Next
WSCript.Echo "Just killed process " & strProcessKill _
& " on " & strComputer
WScript.Quit
' End of WMI Example of a Kill Process

by lowlands

The reason the quote is where it is in Robo's example is because the process is defined as a variable. In Richard's script it is just part of the string.

The code to terminate works perfectly if I replace all with notepad.exe

the piece for the workingset size is flawed though, so I am not sure if that is where it is going wrong, because with the way you have it in your example, it just seems to bypass the code.

I would add some echos after the different steps and see what is missing where. And I don't think the processname is case sensitive, but you might want to play with that.

Also make sure the process name truly shows up as "'Defraggler.exe'", sometimes process names get cut of strangely

This discussion is closed to new replies.