Re: How to detect & kill a task
- From: Bobby <rpqcca@xxxxxxxxx>
- Date: Tue, 10 Mar 2009 06:41:35 -0700 (PDT)
On Mar 10, 9:39 am, Bobby <rpq...@xxxxxxxxx> wrote:
On Feb 22, 7:55 pm, "Pegasus \(MVP\)" <I....@xxxxxxxxxx> wrote:
"Bobby" <rpq...@xxxxxxxxx> wrote in message
news:423137c1-32a2-4417-bd0f-dfb2b6df056f@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Feb 22, 5:21 pm, urkec <ur...@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
"Pegasus (MVP)" wrote:
"Bobby" <rpq...@xxxxxxxxx> wrote in message
news:acf98bc1-5658-48d0-980a-35a919f311de@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi,
I am trying with a simple VBSCRIPT to locate en stop exel.exe from
running! Could someone correct this simple program!
Thanks ahead.
Dim objShell, objExecObject, pid, app
set objShell = CreateObject("wscript.shell")
set objExecObject = objShell.Exec("c:\WINDOWS\system32\QPROCESS..EXE
*")
Do Until objExecObject.StdOut.AtEndOfStream
app = Left(objExecObject.StdOut.ReadLine, 63)
app = right(app,9)
pid = Left(objExecObject.StdOut.ReadLine, 52)
pid = Right(pid, 4)
'WScript.Echo (objExecObject.StdOut.ReadLine)
'WScript.Echo (pid & " " & app)
If app = "EXCEL.EXE" Then
objExecObject = objShell.Exec("c:\WINDOWS\system32\TSKILL.EXE " &
pid)
End If
Loop
There are a couple of problems with your script:
- Executing qprocess.exe requires quite a bit of CPU power.
- The script will end after it has killed Excel.
You might get better results if you rely on WMI. Here is a script based
on
an idea by the Scripting Guy:
sProcess = "Excel.exe"
Set oWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set cMonitoredProcesses = oWMIService.ExecNotificationQuery _
("Select * From __InstanceCreationEvent Within 5 " _
& "Where TargetInstance ISA 'Win32_Process'")
Do
Set oLatestProcess = cMonitoredProcesses.NextEvent
If InStr(1, oLatestProcess.TargetInstance.Name, sProcess, 1) > 0 _
Then WScript.Echo sProcess & " has started."
Loop
You will need to replace [WScript.Echo sProcess & " has started."] with
your
own code to kill Excel. If you use taskkill.exe then you do not really
need
the PID - it is sufficient for you to use /im excel.exe. However, you
should
use the /f switch.
It is not clear if he wants to monitor excel process creation or just to
kill the running excel processes, in which case it would be enough to use
taskkill.exe or this script (from the Script Center):
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process " _
& "Where Name = 'Excel.exe'")
For Each objProcess in colProcessList
objProcess.Terminate()
Next
--
urkec
My blog:http://theadminblog.blogspot.com/
My CodeProject
articles:http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=4...
Hide quoted text -
- Show quoted text -
What I am trying to do is to kill a specific EXCEL file. I do this
verification to make sure that the EXCEL file is close bedore starting
a night process.
Thank you both!
=================
Remember that in such simple cases a humble batch file can be extremely
effecitve:
@echo off
tasklist | find /i "excel" && taskkill /f /im excel.exe
"c:\Program Files\MyApp\MyApp.exe"- Hide quoted text -
- Show quoted text -
Could someone show me how to see thru a msgbox the pid number?
Thanks- Hide quoted text -
- Show quoted text -
Sorry I miss! The code is:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process " _
& "Where Name = 'Excel.exe'")
For Each objProcess in colProcessList
objProcess.Terminate()
Next
.
- References:
- Re: How to detect & kill a task
- From: Bobby
- Re: How to detect & kill a task
- Prev by Date: Re: How to detect & kill a task
- Next by Date: Re: Detect logoff or shutdown
- Previous by thread: Re: How to detect & kill a task
- Next by thread: Help required with Word
- Index(es):
Relevant Pages
|