Problem with script that is attempting to query running processes.

mviking
mviking used Ask the Experts™
on

I need some help. This script is designed to install an application and monitor its set-up process by doing a query of the current running processes. As the application is running the script will loop and identify if the installation is complete. The problem occurs when I run the script to install from an installer shield application with the process title of _isDEL.EXE. It will not pick up that process name in the loop of the script.

Here is where it gets interesting. If I simply comment out the mapping of the drive and the installation part of the script and change the _isDel.exe to notepad.exe the script will run as I have designed it to run. The question is why is my loop in the installation script not picking up the process called _isDel.exe?
I need some advice.....

Dim appChck, prcss, appRun

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcess = objWMIService.ExecQuery ("Select Description from Win32_Process")
Set objShell = WScript.CreateObject("Wscript.Shell")
Set net = WScript.CreateObject("Wscript.Network")

net.MapNetworkDrive "X:","\\servername\applications\Template\Client_Access_Express\Current_5.1\Source"
WScript.Sleep 5000
objShell.run "x:\setup.exe -s"
Wscript.Sleep 5000

Do While appRun <> "TRUE"
appRun = "TRUE"
WScript.Sleep 5000
WScript.Echo("Checking for Application.")
Set colProcess = objWMIService.ExecQuery ("Select Description from Win32_Process")
'MsgBox("query process " & appRun)
For Each objProcess In colProcess
prcss = objProcess.Description
MsgBox(LCase(prcss))
If LCase(prcss) = "_isDEL.EXE" Then
'If LCase(prcss) = "notepad.exe" Then
appRun = "FALSE"
WScript.Echo("Application is still running.")
End If
MsgBox("appRun Is " & appRun)
Next
prcss = "" 
Loop

WScript.Echo("Application is not running.")
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
The offending line of code may be:
If LCase(prcss) = "_isDEL.EXE" Then

It will never match.  Try:
If LCase(prcss) = lcase("_isDEL.EXE") Then

Author

Commented:
twalgrave
Thanks for the quick reply.

This produces the same result.  This is frustrating.  I am running MsgBox(LCase(prcss)) directly before the loop to veryify that LCase can see _isDel.exe within the running list of processes.  Yet, something keeps killing my loop.  The script drops to the end and displays "Application is not running"  when in actuality it is running still in the processes.

Other ideas?
You say something is killing your loop.  Does that mean you are receiving an error?  BTW, I'm not certain by your comment that you actually changed the code that I showed you because you mention the MSGBOX.  Did you change the line in my last response (not the MSGBOX line)?  I would also recommend NOT using msgbox at all.  Send the output to the debug window or a testbox.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

also, you may want to test for INSTR instead of lcase like this:
if instr(1,prcss,"_isDEL.EXE", vbTextCompare) <> 0 then

Author

Commented:
Twalgrave
I have always used msgbox intially to test my code.  How do I send output to the debug window or a testbox?

Author

Commented:
Twalgrave
I have always used msgbox intially to test my code.  How do I send output to the debug window or a testbox?
For debug window.
debug.print LCase(prcss)
To see the debug window press Ctrl-G.

Assuming a Textbox named Text1 and it has the Multiline property set to true.
Text1.text = text1.text & vbcrlf & LCase(prcss)

Author

Commented:
I will work with these suggestions and see if I can't get this code to work.  I will post again
The reason I don't use message boxes is that in some circumstances, it can produce results that differ from what would go on without the messagebox.

For instance:
1) It stops code execution thereby putting a pause that wouldn't be there otherwise
2) You lose focus on controls
3) If you accidentally double-click or hold down the enter key, you may end up pressing something you didn't intend.
mviking intersting script.  I'm trying to write the same feature in C++ and it took loads of lines.  How are you firing the initial script, which script engine are you using?

Author

Commented:
twalgrave,
What do you think of the new VBscript?  Any feedback would be appreciated.




Dim appChck, prcss, appRun
Dim strProcess, objWMIService, objShell, objNet

strProcess = "_isDEL.EXE"

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set objShell = WScript.CreateObject("Wscript.Shell")
Set objNet = WScript.CreateObject("Wscript.Network")

objNet.MapNetworkDrive "X:","\\servername\applications\Template\Client_Access_Express\Current_5.1\Source"
WScript.Sleep 5000
objShell.run "x:\setup.exe -s"
Wscript.Sleep 5000

appRun = TRUE
Do While appRun
WScript.Echo("Checking for Application.")
Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name ='" & strProcess & "'")
If colProcess.Count > 0 Then
appRun = True
WScript.Echo("Application is still running.")
Else
appRun = False
WScript.Echo("Application is not running.")
End If

Set colProcessList = Nothing
WScript.Sleep 5000
Loop
Set objWMIService = Nothing
Set objShell = Nothing
Set objNet = Nothing
Does it work?  I hope so.

A couple of notes (you asked for general feedback so I'm going to treat this as though it were a code review).  Take these suggestions with a grain of salt as when I review code I am pretty strict:

Going down the code I find the following:

1) strProcess should be set as a module-level constant.

2) You should test for the objects that are created with the GetObject function to see if they are set to nothing.  If they are, indicate so to the user and get out of the function.

3) The Sleep function:  I assume it is necessary to allow the mapping of the drive to complete.  Is there a way to test the return value and/or process this simultaneously?  Sleeping for 5 seconds may work today, but when the network is loaded, it may take longer than 5.

4) Do While appRun  should be changed to Do While appRun = TRUE.  You want to make sure you have total control over your code.  It is possibly for someone to poke a new value into the memory space that the appRun variable resides in without your knowledge.  If this happens, your existing condition will still evaluate to not false unless the person poking in memory happens to put Flase in there.

5) I don't see colProcess dimensioned.

6) I like to see the keywords in a query statement in caps.  This helps when reading it.  for example: "SELECT * from Win32_Process WHERE Name ='

7) Along those lines, if you are only testing for the Name value, you should not return all the fields from the query.  Use "SELECT Name from Win32_Process WHERE Name ='

8) I don't know if its possible to do something like:
<("Select * from Win32_Process Where lcase(Name) ='" & lcase(strProcess) & "'")> or if it even matters, but it would be something to try so you can be sure variable case is not an issue

8) I prefer to see variable naming conventions in use.  For example: appRun  becomes bApprun

9) Set colProcessList = Nothing - I don't see this being defined/used anywhere.  I think you mean Set colProcess = Nothing

10) I don't see prcss being used anywhere, remove the declaration

11) You may consider allowing the loop to get out in case too many iterations of it happen.  For example:  Suppose _IsDel.EXE" stops responding.  Your application will now stop responding as well.  Put a maximum number of times to test in the code and get out if you exceed that number of times.


12 Indenting helps termendously and so does white-space (blank lines)

Author

Commented:
Twalgrave, thanks for your insight and education!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial