Terminating a process

Terminating a process

Post by dennishanc » Sun, 04 Jul 2004 00:27:09


I have code in a .wsf file:

Set wshShell = WScript.CreateObject("WScript.Shell")
wshShell.Run RunThis.exe, 0, false

It does some other processing, and before the program ends, it runs
this command:

Set wshShell = nothing



However, when the script ends, RunThis.exe still shows up as an active
process in my Task Manager.

Any idea why this process isn't being killed as a result of the last
command?


Dennis Hancy
Eaton Corporation
Cleveland, OH
 
 
 

Terminating a process

Post by VG9tIExhdm » Sun, 04 Jul 2004 02:18:02

Setting that object to Nothing has no effect because the wshShell is no longer attached to the RunThis.exe thread. The third argument on the Run line made sure that this was the case.

You need to use WMI to either create the thread in the first place or to search for the thread's PID to permit termination under your script's control.

For example, from the Script Center's sample library ...

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'RunThis.exe'")
For Each objProcess in colProcessList
objProcess.Terminate()
Next

There is also a process creation class (Win32_ProcessStartup) that returns the PID directly at the time the thread is created, but it's way complicated to use (I think ;-)

Tom Lavedas
===========

Microsoft Windows2000 Scripting Guide (URL all one line)
http://www.yqcomputer.com/

TechNet Script Center Sample Scripts (URL all one line)
http://www.yqcomputer.com/

 
 
 

Terminating a process

Post by Fosc » Mon, 05 Jul 2004 13:55:05

"Dennis Hancy"

Set oShell = CreateObject("WScript.Shell")
Set oExec = oShell.Exec("calc")
Do While oExec.Status = 0
WScript.Sleep 100
Loop
WScript.Echo "status" &" "& oExec.Status
Set oExec = oShell.Exec("calc")
WScript.Echo "status" &" "& oExec.Status
WScript.Sleep 2000
oExec.Terminate()
 
 
 

Terminating a process

Post by dennishanc » Thu, 08 Jul 2004 03:40:17

Tom,

I tried this code as is (except I changed RunThis.exe to my own exe
file). However, the exe file still shows up in the task manager.

I am not sure I know what WMI is. Do I need to do something special
to make it run? Or will it run as listed? I included some debug
msgbox statements in my code, so I know that it found my exe file.
Just not sure why it still shows up in the task manager.

Thanks.


Dennis
 
 
 

Terminating a process

Post by VG9tIExhdm » Thu, 08 Jul 2004 05:46:01

If you verified that it found your application and you didn't get any error messages, then it ran as expected. I don't know of any limitation on the WMI Teminate process, but I'm no expert. However, every instance I have ever tried has worked fine. Did you try 'refreshing' the Task Manager list after you 'terminated' the application?

Tom Lavedas
===========
 
 
 

Terminating a process

Post by dennishanc » Fri, 09 Jul 2004 04:41:54

'm so confused. Even after 'refreshing' the Task Manager List, the
exe file still shows up. Does this have anything to do with security
settings? This is all new territory for me, so I'm basically
brainstorming here as to what the problem might be.

As a further check, is there anyway I can print out the PID value,
just to confirm that it's finding the right one?

Thanks.


Dennis


"Tom Lavedas" < XXXX@XXXXX.COM > wrote in message news:< XXXX@XXXXX.COM >...
 
 
 

Terminating a process

Post by VG9tIExhdm » Fri, 09 Jul 2004 05:17:01

o 'print out' the name and PID, change this line ...

objProcess.Terminate()

to

wsh.echo objProcess.Name, objProcess.ProcessID

You might also add a last line in your script ...

wsh.echo "Done"

If the first Echo does not appear, it means you have not matched the name of the process in the list WMI keeps. If that is the fact, comment out the Where part of the query line, something like this ...

Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process") 'Where Name = 'RunThis.exe'")

And then run the script at a command prompt using Cscript.exe as the host (otherwise you will be pressing the Enter key a lot to get through all of the processes). Then review the list to see how the file is really named - the query may be case sensitive - I can't remember.

Tom Lavedas
===========


"Dennis Hancy" wrote:

 
 
 

Terminating a process

Post by dennishanc » Fri, 09 Jul 2004 20:56:28

his is getting stranger.

Prior to your last posting, I had put a msgbox statement inside the
"for" loop to prove to myself that it found the process I wanted to
kill. Inside the msgbox, I simply placed a counter. So when it ran,
it printed a 1 on my screen.

Well I now added the echo statement as you suggested. While it still
prints the counter in the msgbox, the echo statement seems to be
getting ignored. This happens even when I comment out the Where
clause.

I'm thinking one of two things could be happening:

1) A syntax error (although I copied/pasted your code)
2) Do I need to declare things like objProcess to anything special?
Right now I only have them in generic Dim statements.

Thanks.


Dennis Hancy
Eaton Corporation
Cleveland, OH
"Tom Lavedas" < XXXX@XXXXX.COM > wrote in message news:< XXXX@XXXXX.COM >...
 
 
 

Terminating a process

Post by dennishanc » Fri, 09 Jul 2004 21:37:37

believe I have solved the problem, although quite honestly, I'm not
sure how.

The only thing I changed was my Dim statements. Originally I had
this:

dim strComputer, objWMIService, colProcessList, objProcess

I changed it to

dim strComputer
dim objWMIService
dim colProcessList
dim objProcess

Didn't think that would make a difference. Perhaps I had a stray
character on the line somewhere?

My code now seems to work just fine. THANK YOU FOR ALL YOUR HELP!!!


Dennis



"Tom Lavedas" < XXXX@XXXXX.COM > wrote in message news:< XXXX@XXXXX.COM >...
 
 
 

Terminating a process

Post by stanscot » Sat, 10 Jul 2004 06:29:12

Dennis,

Glad it's all working. VBScript doesn't allow multiple DIM statements on one line.

Stan Scott
New York City
 
 
 

Terminating a process

Post by MV » Sat, 10 Jul 2004 06:43:16


Yes it does...

E.g.

dim strComputer, objWMIService, colProcessList, objProcess

is perfectly legal.


--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.yqcomputer.com/