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

Re: WshShell.Exec not finishing by allancady

allancady
Sat Dec 04 21:49:07 CST 2004

This is a bug in the Cisco VPN client. Upgrading to version 4.0.5(A)
(from 4.0.2(A)) fixed the problem.

allancady@yahoo.com (Allan Cady) wrote in message news:<d563b154.0412031310.280cd11@posting.google.com>...
> 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

Re: WshShell.Exec not finishing by ()

()
Wed Dec 15 08:48:58 CST 2004

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...

Re: WshShell.Exec not finishing by Torgeir

Torgeir
Wed Dec 15 12:15:18 CST 2004

wrote:

> 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).


> 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<----------------------


> 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

Re: WshShell.Exec not finishing by Eddy

Eddy
Wed Jan 26 21:46:41 CST 2005

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





"Allan Cady" <allancady@yahoo.com> wrote in message
news:d563b154.0412031310.280cd11@posting.google.com...
> 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