We help IT Professionals succeed at work.

How do I  terminate a process based on application name instead of process name in vbs?

robkarlen
robkarlen asked
on
Medium Priority
337 Views
Last Modified: 2012-05-06
I'm looking for the correct syntax in vbs to kill a process based on it's application name in task manager.  My current code kills the process based on it's process name, but the problem now is that it kills all running iexplore processes whereas they require it to just kill one specific instance.
KillApps = "iexplore.exe, Please Wait.exe"
For Each app In Split(KillApps, ", ")
    Set objProcs=GetObject("winmgmts:\\.\root\cimv2").ExecQuery("select * from Win32_Process where Name='" & app & "'")
    For Each process In objProcs
        process.Terminate
    Next
Next
 
WScript.Quit

Open in new window

Comment
Watch Question

Author

Commented:
Would creating a small executable in AutoIT be my best solution?  I don't think it's possible to terminate on anything aside from process or PID.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
It is possible, but it requires that Microsoft Word is installed:

How Can I Close a Command Window with a Specific Title?
http://www.microsoft.com/technet/scriptcenter/resources/qanda/nov05/hey1130.mspx

Regards,

Rob.

Author

Commented:
That's so very close to what I need but unfortunately not all of our machines have Word.  Can you create an object of word viewer and accomplish this by any chance? - it doesn't appear to be a builtin server class  Licensing restrictions do not allow us to have Word on every machine.

Thanks for the help so far, you've got me pointed in a good direction.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
Mmmm, possibly....give this a shot....this gets the task information from running "tasklist -v" and tries to find the window title, and kill that process.

Regards,

Rob.
strWindowTitle = "Active Directory Users and Computers"
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
strTempTasks = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "TempTasks.txt"
strCommand = "cmd /c tasklist -v > """ & strTempTasks & """"
objShell.Run strCommand, 0, True
Set objTasks = objFSO.OpenTextFile(strTempTasks, intForReading, False)
strProcessID = ""
While Not objTasks.AtEndOfStream
	strLine = objTasks.ReadLine
	strTaskTitle = Mid(strLine, 153)
	If Trim(strTaskTitle) = Trim(strWindowTitle) Then strProcessID = Trim(Mid(strLine, 27, 6))
Wend
objTasks.Close
If strProcessID <> "" Then
	strCommand = "cmd /c taskkill /pid " & strProcessID & " /f"
	objShell.Run strCommand, 0, True
	MsgBox strWindowTitle & " with process id " & strProcessID & " has been terminated."
Else
	MsgBox strWindowTitle & " was not found."
End If

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thanks a bunch.  A little tweaking got me exactly what I needed.  I would have never ever thought of this approach so thank you so very much.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Thanks for the grade.

Was the tweaking anything major?  This appeared to work for me OK....

Regards,

Rob.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.