#1 15 Jul 2011 19:02

Registered: 15 Jul 2011
Posts: 2

RetVal = objShell.Run fails to capture error


I have a wrapper script that uses the Run method to invoke CSCRIPT and call another VBScript (parent script calls child script).  This works well with one exception: If the child fails for any reason (syntax error or any error raised by system), the wrapper's variable RetVal as well as Err.Number are both ZERO.  Yet, if I invoke the child "cscript some.vbs", I immediately see the error in the CMD prompt.

Here's the line of code that I am referencing:
RetVal = objShell.Run("cscript some.vbs",10,True)

I assume I am doing something wrong.  Note that I must use cscript to invoke the script, as applied security standards have disassociated ".vbs" extensions with cscript / wscript.

Thanks in advance.

Last edited by Paulie (15 Jul 2011 19:08)


#2 15 Jul 2011 20:31

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 997

Re: RetVal = objShell.Run fails to capture error

How are you catching the errors within 'some.vbs' ?


If you exit the script with
WScript.Quit ErrCode

That should return ErrCode to your main script.


#3 16 Jul 2011 06:51

Registered: 15 Jul 2011
Posts: 2

Re: RetVal = objShell.Run fails to capture error

Thanks for the reply. As it turns out, Syntax Errors are not trapable in this fashion (at Run method), since they occur previous to the script execution.  While it's always good to first thoroughly test a child script, I was hopeful that the Run method was otherwise infallible.


#4 16 Jul 2011 13:48

Registered: 10 Jan 2011
Posts: 48

Re: RetVal = objShell.Run fails to capture error

it seems that you need to trap the exception in the child script not in the parent
to prevent the child script from throwing an exception which will always result in a terminating error
which will lead to nothing in the end,and the retval will expect an explicit error exit code ,so you need to do it manually
in the child script before the line where you suspect there would be an error

like this:

'child script.vbs
'enables error handler in the code below
'code that may cause an error goes here
On Error resume Next 
If Err.Number <> 0 Then
end if
'now we passed where it is suspected that an error may occur
'Disables any enabled error handler for the next lines of code
On Error GoTo 0
'if an error occur here it will terminate the excution of the child script and the retval will get zero


Board footer

Powered by FluxBB