PDA

View Full Version : VBScript WshShell.Run Method


dswimboy
05-24-2004, 09:40 PM
i'm having some trouble with this method. it isn't waiting for outlook to exit.
here is the documentation link, and the code:

Return = WshShell.Run("C:\Progra~1\Outloo~1\msimn.exe", 3, true)

http://msdn.microsoft.com/archive/en-us/wsh/htm/wsMthRun.asp

i realize this isn't much. but it should wait until that the program is done, until executing the next lines of code. and it isn't! if you want me to post all my code, let me know.

Antoniohawk
05-24-2004, 11:26 PM
Did you create the object as shown in the code? We'll probably have to see more code to be able to help you any.

dswimboy
05-25-2004, 06:26 PM
dim objWMI, objProcess, strProcesses, WshShell, Ret

Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}")
Set objProcess = objWMI.InstancesOf("Win32_process")
set WshShell = WScript.CreateObject("WScript.Shell")

For each Process in objProcess
strProcesses = strProcesses & Process.Name & vbcrlf
Next

Dim regEx, Match, retVal ' Create variable.
Set regEx = New RegExp ' Create a regular expression.
regEx.Pattern = "sb_tray.exe" ' Set pattern.
regEx.IgnoreCase = True ' Set case insensitivity.
retVal = regEx.Test(strProcesses)


If retVal Then
Else
WshShell.Exec "C:\Progra~1\SpamBayes\bin\sb_tray.exe"
WScript.Sleep 500
End If

Ret = WshShell.Run("C:\Progra~1\Outloo~1\msimn.exe", 3, true)

Set objProcess = objWMI.InstancesOf("Win32_process")
For each Process in objProcess
If (instr(1,lcase(Process.name),lcase("sb_tray")) > 0) then
Process.terminate 0
End If
Next

it wask working for a while, and then it randomly stopped...i have no idea what's up. sb_tray.exe starts, then OE starts, then sb_tray.exe terminates without waiting for OE to exit.

swmr
05-26-2004, 07:41 AM
WSH can halt script execution at the line of Run(), but has no such control over program execution.
Article: Running Programs From WSH Scripts (http://www.microsoft.com/technet/community/columns/scripts/sg1002.mspx)

dswimboy
05-27-2004, 03:16 AM
from MSDN (http://msdn.microsoft.com/library/en-us/script56/html/wsMthRun.asp?frame=true)
bWaitOnReturn
Optional. Boolean value indicating whether the script should wait for the program to finish executing before continuing to the next statement in your script. If set to true, script execution halts until the program finishes, and Run returns any error code returned by the program. If set to false (the default), the Run method returns immediately after starting the program, automatically returning 0 (not to be interpreted as an error code).

it also worked at one point. i never changed the script. it stopped working sometime this month. i don't remember significant changes. i didn't have the time to troubleshoot when i first noticed the errors though. could be an update, or some other program. does anyone know a work-around?

swmr
05-27-2004, 04:29 PM
...i have no idea what's up. sb_tray.exe starts, then OE starts, then sb_tray.exe terminates without waiting for OE to exit.
I told you what's up. :rolleyes:

swmr
05-27-2004, 08:29 PM
Try adding something like WScript.Echo("blah") immediately after the Run method.

That should tell you whether sb_tray.exe is terminating by itself.

Meaning that if you haven't exited OE, and there has been no Echo, then its not your script that's terminating sb_tray.exe, prematurely.

dswimboy
05-28-2004, 04:09 AM
i've checked the Task Manager. sb_tray launches, OE launches, sb_tray exits.

swmr
05-28-2004, 04:53 AM
Just the fact that sb_tray is exiting doesn't mean that your script has continued, and terminated it.

Is bWaitOnReturn failing, or not?

dswimboy
05-28-2004, 04:58 AM
it is failing. i get an echo.

swmr
05-28-2004, 05:05 AM
Well, that sucks. :D

swmr
05-28-2004, 05:18 AM
You could try checking for the OE process, before terminating sb_tray, and if OE is still running, tell WScript to sleep for a few seconds and then check again...

swmr
05-28-2004, 08:29 AM
I'm not too proficient in vbs, but this might convey what I meant: ;)


dim WshShell, objWMI, objProcess, Ret

Set WshShell = WScript.CreateObject("WScript.Shell")
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}")
Set objProcess = objWMI.ExecQuery _
("SELECT * FROM Win32_Process WHERE Name = 'sb_tray.exe'")

If objProcess.Count = 0 Then
WshShell.Exec "C:\Progra~1\SpamBayes\bin\sb_tray.exe"
WScript.Sleep 500
End If

Ret = WshShell.Run("C:\Progra~1\Outloo~1\msimn.exe", 3, true)

confirmExit

Sub confirmExit()
Set objProcess = objWMI.ExecQuery _
("SELECT * FROM Win32_Process WHERE Name = 'msimn.exe'")
If not objProcess.Count = 0 Then
WScript.Sleep 5000
confirmExit
Else
Exit Sub
End If
End Sub

Set objProcess = objWMI.ExecQuery _
("SELECT * FROM Win32_Process WHERE Name = 'sb_tray.exe'")
For Each proc in objProcess
proc.Terminate()
Next

dswimboy
05-28-2004, 05:57 PM
thanks for the workaround. i'll implement it this weekend and let you know how it goes.