Discussion:
WshShell.Exec not finishing
(too old to reply)
Allan Cady
2004-12-03 21:10:14 UTC
Permalink
Why would a process that runs to completion when run directly or from
a DOS batch file, fail to finish when run using WshShell.Exec?

I'm trying to run a VPN client program from VBScript, using the
WshShell.Exec command:

Set exec = wsh.Exec("vpnclient connect MyServer")
Do While (exec.Status = WshRunning)
WScript.Sleep 100
Loop

I need a way to test whether the connection was successful. When
running the client from a batch file, this can be done by testing
ERRORLEVEL. For example, a batch file containing this:

vpnclient connect MyServer
ECHO Command returned ERRORLEVEL: %ERRORLEVEL%

exits after a couple of seconds, and echoes "200" if the connection is
successful.

Presumably, I should be able to access this exit code from VBScript
using the ExitCode property of the exec object. The problem is, when
I run the script, exec.Status never changes from WshRunning (0).
According to the MS documentation, "If the process has not finished,
the ExitCode property returns 0", so I can never check the exit code.

Can anyone suggest why the Exec never finishes, and if there might be
some workaround?

Thanks,

Allan
Allan Cady
2004-12-05 03:49:07 UTC
Permalink
This is a bug in the Cisco VPN client. Upgrading to version 4.0.5(A)
(from 4.0.2(A)) fixed the problem.
Post by Allan Cady
Why would a process that runs to completion when run directly or from
a DOS batch file, fail to finish when run using WshShell.Exec?
I'm trying to run a VPN client program from VBScript, using the
Set exec = wsh.Exec("vpnclient connect MyServer")
Do While (exec.Status = WshRunning)
WScript.Sleep 100
Loop
I need a way to test whether the connection was successful. When
running the client from a batch file, this can be done by testing
vpnclient connect MyServer
ECHO Command returned ERRORLEVEL: %ERRORLEVEL%
exits after a couple of seconds, and echoes "200" if the connection is
successful.
Presumably, I should be able to access this exit code from VBScript
using the ExitCode property of the exec object. The problem is, when
I run the script, exec.Status never changes from WshRunning (0).
According to the MS documentation, "If the process has not finished,
the ExitCode property returns 0", so I can never check the exit code.
Can anyone suggest why the Exec never finishes, and if there might be
some workaround?
Thanks,
Allan
unknown
2004-12-15 14:48:58 UTC
Permalink
Hi Allan,

I don't have an answer, but I am having a similar problem, and I have a *little* more detail, and some suggestions at the bottom..

I created a small test script to launch Notepad. I Echoed both the ExitCode and Status properties of the resulting Exec object, then slept for 10 seconds, and echoed the same properties. (The 10 second sleep gave me a chance to manually close Notepad). The first Echo showed a status of 0 (WshRunning), and an ExitCode of 0, which is as expected. After closing Notepad, Status changed to 1 (WshFinished), but ExitCode remained 0.

I am now of the opinion that the ExitCode property does not work. I have several references for WSH/VBscript (2 of which come directly from Microsoft), and I have only found reference to ExitCode in one place (sounds like the exact same reference you saw). However, within the same reference book, I looked at the entry for "WshScriptExec Object", and there is no mention of an ExitCode property; only Status, StdOut, StdIn, StdErr, and the Terminate method.

Perhaps you could:
a) Query the StdOut & StdErr properties of your exec object instead of ExitCode, and look for significant strings (I don't know if the vpnclient connect command even writes to StdOut or StdErr).

b) Use Exec to run the batch file you mentioned and query *its* StdOut for the ERRORLEVEL that is echoed.

c) Query the Event Log(s) for significant strings (again, I don't know if vpnclient even writes anything here).

d) Try using the WshShell.Run method instead of Exec.

**********************************************************************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
Torgeir Bakken (MVP)
2004-12-15 18:15:18 UTC
Permalink
Post by unknown
I don't have an answer, but I am having a similar problem, and I
have a *little* more detail, and some suggestions at the bottom..
I created a small test script to launch Notepad. I Echoed both the
ExitCode and Status properties of the resulting Exec object, then
slept for 10 seconds, and echoed the same properties. (The 10
second sleep gave me a chance to manually close Notepad). The
first Echo showed a status of 0 (WshRunning), and an ExitCode of 0,
which is as expected. After closing Notepad, Status changed to 1
(WshFinished), but ExitCode remained 0.
That is to be expected, because Notepad.exe will always return 0
when exiting (it has no reason to set an error level).
Post by unknown
I am now of the opinion that the ExitCode property does not work.
It does work, but you need to run a process that sets the error
code to something else than 0 when exiting, like the script below.

'--------------------8<----------------------
Set oShell = CreateObject("WScript.Shell")
Set oExec = oShell.Exec("%comspec% /c echo no | find ""yes""")

Do While oExec.Status = 0
WScript.Sleep 100
Loop

WScript.Echo oExec.ExitCode
'--------------------8<----------------------
Post by unknown
I have several references for WSH/VBscript (2 of which come
directly from Microsoft), and I have only found reference to
ExitCode in one place (sounds like the exact same reference you
saw). However, within the same reference book, I looked at the
entry for "WshScriptExec Object", and there is no mention of an
ExitCode property; only Status, StdOut, StdIn, StdErr, and the
Terminate method.
Take a look at this documentation by Mike Whalen (he was in the WSH 5.6
development team):

http://groups-beta.google.com/group/microsoft.public.scripting.wsh/msg/18e3f3091acb4274?dmode=source

Exec.ExitCode - When the process is done, this will store the exit code from
the process (i.e., the %ERRORLEVEL%). Until Exec.Status is 1, this value
will be 0.
--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx
Eddy Bacon
2005-01-27 03:46:41 UTC
Permalink
Allan,

Sorry you had to wait so long for a response.

Does the VPN client program actually connect? - ie - how do you verify the
connection
when it connects though the batch file? Do you actually get an errorlevel
in DOS?

If the vpn program is custom written, it may not return properly (void or
null) to Windows (no errorcode).

As far as I can tell you are using the Status property correctly.

Eddy
Post by Allan Cady
Why would a process that runs to completion when run directly or from
a DOS batch file, fail to finish when run using WshShell.Exec?
I'm trying to run a VPN client program from VBScript, using the
Set exec = wsh.Exec("vpnclient connect MyServer")
Do While (exec.Status = WshRunning)
WScript.Sleep 100
Loop
I need a way to test whether the connection was successful. When
running the client from a batch file, this can be done by testing
vpnclient connect MyServer
ECHO Command returned ERRORLEVEL: %ERRORLEVEL%
exits after a couple of seconds, and echoes "200" if the connection is
successful.
Presumably, I should be able to access this exit code from VBScript
using the ExitCode property of the exec object. The problem is, when
I run the script, exec.Status never changes from WshRunning (0).
According to the MS documentation, "If the process has not finished,
the ExitCode property returns 0", so I can never check the exit code.
Can anyone suggest why the Exec never finishes, and if there might be
some workaround?
Thanks,
Allan
Loading...