1

I'm currently starting a VBScript from another VBScript via WScript.Exec without waiting for its result (which is working just fine):

Set ObjShell = CreateObject("WScript.Shell")
Set ShellExec = ObjShell.Exec(strFinalCommand)
Set ShellExec = Nothing
Set ObjShell = Nothing

I've just discovered a problem if the second script (which has its own error handling inside) does have a syntax issue.

Since this is determined before the script starts running, I can't notice the error in the second script but instead have to handle it in the first one.

Is there any possibility to check if the call threw an error at the start and - if not, just let it work - if there is an error, stop the wscript and get the error text.

I haven't found anything on such a specific topic so far.

I've already tried ShellExec.ExitCode and ShellExec.StdErr, but are both are only updated after the second script finishes. If there is an error, both are not updated. I also tried ShellExec.SdtErr.AtEndOfStream, which crashed my process if an error appeared.

Edit: The Command I'm running is wscript.exe D:\\...\RunAsyncShell.vbs /ParamOne:123 /ParamTwo:ABC with about 10 params more to follow (can't show the exact call due to company regulations).

  • 1
    Please show the command you're running. Also note that Exec runs the command asynchronously, i.e. does not wait for the external command to return. I'd recommend using Run instead, unless you have very specific reasons for using Exec. – Ansgar Wiechers Jun 12 at 9:54
  • Added the command. I did not use run since I didn't want to wait on any return message, the script is supposed to run asynchronously. i just want to get a not if there are any errors during the call of the second script, before the code itself is running. Right at the point where the second script starts, I want the first one to leave it be and continue. – DaJohn Jun 12 at 12:40
0

You could do something like this:

Set ObjShell = CreateObject("WScript.Shell")
Set ShellExec = ObjShell.Exec(strFinalCommand)

limit = DateAdd("s", 2, Now)
Do While ShellExec.Status = 0 And Now < limit
    WScript.Sleep 100
Loop

If ShellExec.ExitCode <> 0 Then
    WScript.Echo "Script failed."
End If

That will wait up to 2 seconds after launching the second script, and report an error if the process returned a non-zero exit code within that time frame. However, you'll probably have to run the second script with cscript.exe instead of wscript.exe, because the latter will display a popup when a syntax error occurs, and the process will not terminate before you click that popup away.

  • I've already tried a 200ms sleep but still got the ExitCode = 0, but with wscript. I will give your suggestion with cscript a try, that popup might have been the problem. – DaJohn Jun 13 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.