Guy’s Scripting Ezine 72 Error Correcting Code

Contents for Ezine 72 Error Correcting Code


This Week’s Secret

I admit that sometimes, I too copy and then amend other people’s scripts.  However, for this week’s script, I have developed a new VBScript routine that I have not seen elsewhere.  The idea is to create a sub-routine that traps errors and then displays a custom message.  Incidentally, this leads me to wonder when a script becomes and applet, and when an applet becomes encased in a fancy front end and so becomes a program.  I confess to a mercenary disposition, as one of my dreams is to write and sell mini scripting programs.

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

This Week’s Mission – To create error correcting code

Mapping a network drive is the scenario for our error correcting code.  Let us assume that there was a mistake in our VBScript, for example a mistyped servername.  What happens is that the error causes VBScript to halts with a 800xxxxx message.  However, we can do better than a WSH error message, we can: a) create our own message and, b) tell the script to carry on.  To make this scenario realistic, I ask this week’s script to map two network drives.  If the first drive mapping fails then instead of stopping, there is no reason why the script cannot display a custom message and then attempt to map the second drive.

Customizing the error messages.

I have always disliked calculators.  When it comes to Maths, I either create a spreadsheet, or I use the mental arithmetic skills that were drummed into me many, many moons ago.  Nevertheless, in the case of error correcting code the calculator taught me a wonderful lesson.  When I started scripting, I believed there were two separate worlds, the world of hex numbers like 800704B3 and the world of decimals like -2147023693.  It was a truly magic moment when the calculator showed me that 800704B3 and -2147023693 were one and the same.

The point of this story is that when you trap VBScript errors with err.number you get the decimal version.  However, if you let the script run without On Error Resume Next, you get the equivalent error, but as hex 800xxxxx.

I suggest that there are three jobs for error correcting code.  At it’s simplest and crudest, error correcting code just says, forget the error and carry on.  Today we concentrate on the second job of error correcting, namely generating messages that are more meaningful.

The highest level of error correcting would be to take remedial action and avoid or work around the error.  To be fair, the ability of a VBScript to take action depends on the problem, if the server is down, then it’s hard for the script to bring it on line.  Should you misspell the sharename, then the script could take a guess at an alternative, but the result is unlikely to be what you desired.  In case you think I am being pessimistic, here is an example where the script could take remedial action.  If the problem is that the drive letter is already connected then the script could say, disconnect.  Alternatively, the script could then map to a different letter – that would be a great example of defensive coding.

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets.  IPAT cracks this problem of allocating IP addresses in networks in two ways:

For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges. 

For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker

Select Case

I have featured Select Case in previous ezines.  Briefly, much as I love the construction, If… then… else Endif, it gets cumbersome after about the 4th else.  Select Case is an elegant replacement for scripts that have multiple options.  I really don’t know why, but I thought that Select Case would require the ‘Cases’ to be in numeric or alphabetic sequence.  It was a real moment of triumph when Select Case correctly interpreted my random order of error messages.

The knack to scripting Select Case is realizing that you need two variables.  One (errLogonScript), which I think of as the outer loop variable, and one (strError), which acts as a repetitive inner loop variable.  This inner loop variable returns a value and it is this value that the script echoes to a custom message box.  In other circumstances, you could employ this return value to overcome the problem, for example, map to a different drive letter.

Example – To test error correcting code on MapNetworkDrive

Please take a minute to empathise with the purpose of the following error correcting code.  Appreciate the conflict between wanting MapNetworkDrive to work, and wishing that it would fail.  After all, without a failure, we cannot practice our error correcting code.  Our objective is to map two drives, Q: and V:.  However, one UNC path should be real, and the other should induce an error.

Instructions for multiple Mapped Network Drives

  1. Copy and paste the script below into notepad.
  2. Make sure that you have at least one, network share on your server.
  3. Crucially, make a deliberate mistake on the second strRemotePath.
  4. Save the file with a .vbs extension e.g. SelectError.vbs.
  5. Double click the script and expect to see a custom error message box.
  6. Check to see which, if any, drives were actually mapped.

‘ SelectError.vbs – Map Network Drive to Q: and V:
‘ VBScript to test error correcting code with Select Case.
‘ Author Guy Thomas
‘ Version 4.2- May 8th 2005
‘ ——————————————————–‘
Option Explicit
Dim objNetwork, strRemotePath1, strRemotePath2, strError
Dim strDriveLetter1, strDriveLetter2 , errLogonScript
err.number = vbEmpty
strError = 2
strDriveLetter1 = "Q:"
strDriveLetter2 = "V:"

‘ Try one real UNC and one deliberate mistake
strRemotePath1 = "\\lucy4\data\"
strRemotePath2 = "\\alan\zdownloads"
Set objNetwork = CreateObject("WScript.Network")
‘ Here is the one line error correcting code
On Error Resume Next

‘ Section which maps two drives, Q: and V:
objNetwork.MapNetworkDrive strDriveLetter1, strRemotePath1
call GuyCase ‘ Sub Routine Below
objNetwork.MapNetworkDrive strDriveLetter2, strRemotePath2
call GuyCase

‘ Sub Routine
Sub GuyCase()
‘ Select Case (Not Case Select!)
errLogonScript = err.number
Select Case errLogonScript
Case vbEmpty strError = "No Problem "
Case -2147024811 strError = "Drive already in use "
Case -2147023665 strError = "Check server name "
Case -2147023693 strError = "Check share name "
Case -2147024829 strError = "Another share name problem "
‘ Finish with Case Else in order to catch exceptions
Case Else strError = "Research this number: "
End Select
WScript.Echo strError & err.number
‘ Wscript.Echo "Errors = " ‘& err.count
err.number = vbEmpty
End Sub

‘ End of example VBScript

Learning Points

Note 1:  The statement: On Error Resume Next is an essential first step in all error correcting code.

Note 2:  Appreciate the role of err.number in getting a handle on the WSH message box error.  Realize that WSH displays the hex equivalent of the decimal numbers in the script, e.g. 800700B3 = -2147023693

Note 3:  See how Select case employs two variables, errLogonScript and strError

Note 4:  This script seeks to map two network drives Q and V.  To save space, I created a sub routine to control the error correcting code.

Challenge:  Create your own custom messages, send me your best ideas for Case Select and error correcting code.

Summary Error Correcting Code

There are three reasons to create error correcting code.  On this page we focus on employing Select Case to produce custom error messages.  The other reasons for defensive coding are, if the script fails, adding the statement On Error Resume Next will salvage any useful code.  At its best, error correcting code produces, ‘work arounds’, for example mapping to a different drive letter if the first one is in use.

See more about VBScript error correcting codes

VBScripts  • 800 Code list  • Ezines  • Logon Scripts   • PowerShell ErrorAction  • VM Monitor

Ezine 72 Error correcting  • Ezine 107 Error correcting  • Tool Kit   • WMI Monitor Review

Ezine 96 Errors  • Ezine 117 Troubleshooting pure  • Ezine 130 VBScript codes