CScript/WScript Prevent an error from being blocking
Asked Answered
N

6

6

Currently, WScript pops up message box when there is a script error. These scripts are called by other processes, and are ran on a server, so there is nobody to dismiss the error box.

What I'd like is for the error message to be dumped to STDOUT, and execution to return the calling process. Popping as a MSGBox just hangs the entire thing.

Ideas?

Nutter answered 18/9, 2008 at 2:26 Comment(0)
C
4

This is how you should be running Script batch jobs:

cscript //b scriptname.vbs
Cupcake answered 16/10, 2008 at 8:15 Comment(1)
See also #8678941Tetragon
B
2

Don't use WScript; use CScript. At the Windows command prompt, type the following to display help.

cscript //?
I suggest the following:
cscript //H:CScript
This will make CScript your default scripting interpreter. CScript prints messages to the console (i.e., stdout) as you desire. (It does not use dialog windows.) You may also want to try the //B switch, but I can't tell if that has to be run per-script or not. If it is a persistent, one-time switch like the //H switch is, then this may work for you; if not, you may need to modify all of your remote programs to include it. From the information you provided, I think just changing the default interpreter (//H) will do what you want.

You will also need to add some sort of error handling to keep the script from terminating on an error. In Visual Basic Scripting Edition, the easiest thing to do if you just want to ignore errors is to add the following to the top of your script.

On Error Resume Next
See http://msdn.microsoft.com/en-us/library/53f3k80h(VS.85).aspx for more information.
Boss answered 18/9, 2008 at 3:41 Comment(0)
T
1

Use WScript.Echo instead of MsgBox. And also run the script using Cscript instead of Wscript.

Tomy answered 18/9, 2008 at 19:38 Comment(0)
B
0

You haven't stated what language you're using. If you're using VBScript, you can write an error handler using the On Error... statement. If you're using JScript, you can use a try {} catch (x) {} block.

Broody answered 18/9, 2008 at 2:34 Comment(0)
A
0

don't do this:

vbscript: On error resume next

english: "when you have an error, ignore it & just keep going".

Asclepiadaceous answered 18/9, 2008 at 3:22 Comment(0)
S
0

I suggest you put your script code in a Sub - e.g. DoWork - and code your script something like:

On Error Resume Next

DoWork

If Err.Number <> 0 Then

    If "CSCRIPT.EXE" = UCase( Right( WScript.Fullname, 11 ) ) Then
        WScript.StdErr.Write Err.Number & ": " & Err.Description
    Else
        WScript.Echo Err.Number & ": " & Err.Description
    End If
    WScript.Quit 1
End If

Private Sub DoWork

    ... your code ...

End Sub

In this way, when you run your script using cscript //b, and it fails, you'll get an error message output to stderr and the caller will receive a non-zero errorlevel.

Swett answered 1/9, 2011 at 7:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.