Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find process

Status
Not open for further replies.

dali17

Programmer
Sep 13, 2012
3
RO
Does anyone know how to find process for a specific application?
Thank you

 
[wavey]
bulle

This Sample Code show you how to find any application to be Killed.
So the script is looking for any running browsers applications and killed them.
So it's easy to modifie it as you want to do for. Just you change at those lines what the application do you looking for like

Code:
FindApp("opera.exe")
FindApp("iexplore.exe")
FindApp("FireFox.exe")
FindApp("chrome.exe")
FindApp("Safari.exe")

Code:
'FindApp+Kill_All.vbs
 Const ForAppending=8
  'Check if Wscript is running, we exit, otherwise we run the script
  'Vérifie si Wscript est en cours d'exécution, on quitte, sinon on lance le script
  If CheckIfWscriptIsRunning Then Wscript.Quit

     WaitAndLoopForApp(10) '10 is the Number of seconds to wait to find an
	 '  application to be Killed. We can increase this time.
	 '  10 est le Nombre de secondes d'attente pour rechercher une
     '  application à terminer. On peut augmenter cette durée.
Sub FindApp(strApp)
  Set fso = Wscript.CreateObject("Scripting.FilesystemObject")
  set fich = fso.OpenTextFile("C:\Events.log",ForAppending,true) ' Fichier journal
  Set SRVC = GetObject("winmgmts:\\")
  Set ObjServ = SRVC.InstancesOf("Win32_Process")
  For Each myObj In ObjServ
     If LCase(myObj.Name) = LCase(strApp) Then
	    'Writing in the custom log file
        'Ecriture dans le fichier journal personnalisé
        Fich.Write myObj.Name +" is running but killed at : " & Now & vbnewline
		' We close the application and we get out of the loop
        ' On ferme l'application et on sort de la boucle
        IF Not (myObj Is Nothing) Then myObj.Terminate() 
        Exit For
     End If
  Next
  Set ObjServ=Nothing
  Set SRVC = Nothing
End Sub

	
Sub WaitAndLoopForApp(ByVal Sec)
  start=timer
  do
   i=start+timer
 loop until i-start>=Sec  'We wait a number of secodes to act ' On attend un nombre (sec) de secondes pour agir
 If i-Start>=Sec then 
  Do
    FindApp("opera.exe")
    FindApp("iexplore.exe")
    FindApp("FireFox.exe")
    FindApp("chrome.exe")
    FindApp("Safari.exe")
    Wscript.Sleep 10000
    Set WS=CreateObject("Wscript.Shell")
    Com = "%SystemRoot%\System32\CScript.exe " & Wscript.ScriptFullName
    Result=WS.Run(Com,0,True)
  Loop
 End If
End Sub

Function CheckIfWscriptIsRunning()  ' Vérifie si Wscript tourne ou non
  Set SRVC = GetObject("winmgmts:\\")
  Set ObjServ = SRVC.InstancesOf("Win32_Process")
  s=""
  For Each myObj In ObjServ
     If LCase(myObj.Name) = "wscript.exe" then
       s= s+ myObj.name + VbCrLf
     End If
    OK=InsTr(1,s,VbCrLf+"wscript.exe",1) '>=1
  Next
  CheckIfWscriptIsRunning=OK
End Function
 
your script kilss the process and you type the process name into the script. I want to type the application name that appears in task manager and the scprit must find the process name and kill him :) i don't want to type the process name. I see that in task manager if you right click on the app and you select "Go to process" it will take you directly to the respective application process. can i do that in vbs?

thank you
 
I don't think you can in VBS. However, you can use the cmdline TaskList in verbose mode via .exec and parse the results.

Code:
set objShell = CreateObject("Wscript.Shell")
set objWMI = GetObject("winmgmts:\\")

strFind = inputBox("What application do you want to terminate?")

set objExec = objShell.Exec("%comspec% /c tasklist.exe /v")
do until objExec.StdOut.AtEndOfStream
	strLine = objExec.StdOut.ReadLine
	if (instr(lcase(strLine), lcase(strFind))) then
		msgbox strFind & " was found in line:" & vbNewLine & strLine
		strProcess = trim(left(strLine, inStr(strLine, " ")))
		msgbox "The process name is " & strProcess
		if (msgbox("do you want to kill " & strProcess, vbYesNo) = vbYes) then
			set objProcess = objWMI.InstancesOf("Win32_Process")
			for each objItem in objProcess
				if (lcase(objItem.Name) = lcase(strProcess)) then
					msgbox "killing " & objItem.Name
					objItem.terminate
				end if
			next
		end if
	end if
loop

-Geates

"I do not offer answers, only considerations."
- Geates's Disclaimer

 
Does anyone know how to find process for a specific application?
what about sysinternals process explorer.
if you want to kill all processes of an app just use
taskkill /F /IM Outlook.exe /T

MCITP:EA/SA, MCSE, MCSA, MCDBA, MCTS, MCP+I, MCP
 
I think the OP asked for a way to terminate a process by Application name, not by process name. For example, the application 'IBM iSeries Client Access Terminal Emulation' has a process name of 'pcsws.exe'.

-Geates

"I do not offer answers, only considerations."
- Geates's Disclaimer

 
Dali17, the code I posted above allowed me to terminate by application name. Tasklist outputs a verbose list of tasks and associated properties - one of which is application name. The application name is "linked" to the process name by parsing the text of the output. Then, the process name is eventually used to terminate the application.

-Geates

"I do not offer answers, only considerations."
- Geates's Disclaimer

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top