[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 316
  • Last Modified:

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

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

0
robkarlen
Asked:
robkarlen
  • 3
  • 3
1 Solution
 
robkarlenAuthor 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.
0
 
RobSampsonCommented:
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.
0
 
robkarlenAuthor 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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

0
 
robkarlenAuthor 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.
0
 
RobSampsonCommented:
Thanks for the grade.

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

Regards,

Rob.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now