Re: How to detect & kill a task

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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
.



Relevant Pages

  • Re: How to detect & kill a task
    ... pid = Right ... The script will end after it has killed Excel. ... What I am trying to do is to kill a specific EXCEL file. ...
    (microsoft.public.scripting.vbscript)
  • Re: LTT user input
    ... and to that end have taken the existing trace infrastructure ... script for that event (if there's a handler defined for the event ... This gives the script a chance to do whatever Perlish thing it ... syscall totals to individual syscall totals for each pid. ...
    (Linux-Kernel)
  • Re: Using VBS Arrays As Data Ranges In Excel
    ... thanks for the tip on Excel ... Let me give the updated script a try and see how it works out tomorrow... ... Should I be creating the Excel object ... Dim objExcelApp ' Excel application object ...
    (microsoft.public.scripting.vbscript)
  • Re: Excel macro (long)
    ... You should ask this in an Excel programming forum. ... > I found a vbs script that pulls in the time a user logs in to the network ... > Dim objRootDSE, strConfig, objConnection, objCommand, strQuery ... > On Error GoTo 0 ...
    (microsoft.public.word.vba.beginners)
  • RE: Export options
    ... I'm trying to implement your code in my VB app, and I think it will work - ... - but my VB6 is more than a little rusty, ... to do it from this script anyway. ... manipulates each Excel file using that object's methods etc. ...
    (microsoft.public.excel.programming)