'------------------------------------------------------------------------ ' errmgt_v2.vbs ' ' Error handling management routine to be added at the end of a VBscript. ' ' Usage: call errmgt(action,severity) ' ' Two params should be defined in the caller source: the action performed and the severity level. ' If error is not fatal, calling code execution is resumed, otherwise procedure stops. ' ' Note: caller script should be called via Cscript instead of Wscript. ' ' x1.0-0 15-oct-2010 DTL Didier.Morandi at gmail dot com ' x1.1-0 03-nov-2010 DTL add conversion of error number to Hex format ' x2.0-0 03-nov-2010 DTL add journalling '- sub errmgt(WshAction,severity) Dim err_nr, err_desc, err_src Dim strLogfile, strData, objFSO, outFile, i Dim strLine(4) Const ForAppending = 8 strLogfile = "c:\vbs\log\log.txt" if Err.Number < 0 then err_nr = Hex(Err.Number) else err_nr = Err.Number end if if WshAction = "" then WshAction = "[text missing]" err_desc = err.description if err_desc = "" then err_desc = "[text missing]" err_src = err.source if err_src = "" then err_src = "[text missing]" if Err.Number <> 0 then on error goto 0 wscript.echo "WSH>> Error occurred" set objFSO = CreateObject("Scripting.FileSystemObject") if severity = 0 then exit sub elseif severity = 1 then exit sub elseif severity = 2 then strLine(0) = "WSH Action : " & WshAction strLine(1) = "WSH Error nr : " & err_nr strLine(2) = "WSH Description: " & err_desc strLine(3) = "WSH Source : " & err_src ' strLine(4) = "Procedure resumed." set outFile = objFSO.OpenTextFile(strLogfile, ForAppending, true) for i = 0 to 4 strData = now & " - " & wscript.scriptName & " - " & strLine(i) outFile.WriteLine(strData) wscript.echo strLine(i) next outFile.Close elseif severity = 4 then strLine(0) = "WSH Action : " & WshAction strLine(1) = "WSH Fatal error: " & err_nr strLine(2) = "WSH Description: " & err_desc strLine(3) = "WSH Source : " & err_src strLine(4) = "Procedure terminated abnormally." set outFile = objFSO.OpenTextFile(strLogfile, ForAppending, true) for i = 0 to 4 strData = now & " - " & wscript.scriptName & " - " & strLine(i) outFile.WriteLine(strData) wscript.echo strLine(i) next outFile.Close wscript.quit 1 else wscript.echo "Invalid Severity parameter passed to Error Handler" & vbcrlf end if Err.Clear end if end sub '------------------------------------------------------------------------ 'eof errmgt.vbs