Guy’s Scripting Ezine 71 – On Error Resume Next

Contents for Ezine 71 On Error Resume Next

This Week’s Secret

It’s an open secret that I am fond of WScript.echo. I waste no opportunity in creating message boxes to confirm my script’s success or to display error messages.  As VBScript is short on debugging tools, WScript.Echo is a valid troubleshooting tool.  However, I have over-used the WScript.Echo technique, so next week alternative strategies to handle errors.  Meanwhile, this week in our second mission, I have an alternative method to display success.  This is my idea, when MapNetworkDrive succeeds, it opens explorer at the very drive that it has just created. Clever.

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

This Week’s First Mission – On Error Resume Next

Our first mission is to map two (or more) network drives.  The scenario is that one of the drive letters is already in use, perhaps we have already run the script once before.  Your main learning point is to troubleshoot with On Error Resume Next.

When VBScript comes to a line of code that it cannot execute, it stops.  In its death-throw, WSH (Windows Script Host) issues an error message in a box.  Actually, this message box is full of useful troubleshooting information, an error number beginning 800xxxxx, the line number where it failed and Microsoft’s built in explanation of what caused the script to halt.  But what if you did not want a message box?  What if wanted the script to ignore the error and continue.  Just because WSH failed to interpret one particular line, does not mean that it cannot continue and map the next network drive.  To avoid the WSH box and check if the script can process more code, add the line: On Error Resume Next.

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.

Example 1 – To Map Multiple Network Drives

Our objective is to map two drives, M: and P:.  The corresponding shares could be held on a Windows server, or for testing, you could use any Microsoft machine later than Windows 95.

Pre-requisites

  1. On Line 14 change the server name from ‘\\alan’ to your server name.
  2. Make sure that your server has a share called ‘\drivers’.  Alternatively, edit the sharename in the script, and type in the name of your UNC path.

Instructions for multiple Mapped Network Drives

  1. Copy and paste the script below into notepad.
  2. Did you edit the server name from "\\alan to the name of your server?
  3. Make sure that you have one, network share on your server.
  4. Crucially, leave in the deliberate mistake \\nowhere\nochance
  5. Save the file with a .vbs extension e.g. TwoDrives.vbs.
  6. Double click the script and expect to see a Windows Script Host error message box.
  7. To get rid of the message, box and to actually map the second drive, uncomment line 17: On Error Resume Next.

‘ TwoDrives.vbs – Map Network Drive to M: and P:
‘ VBScript to Demonstrate On Error Resume Next
‘ ——- N.B. remove the comments on line 17 ———
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 1.2 – April 24th 2005
‘ ——————————————————–‘
Option Explicit
Dim objNetwork, objShell, strRemotePath1, strRemotePath2
Dim strDriveLetter1, strDriveLetter2, strDriveExplore

strDriveLetter1 = "M:"
strDriveLetter2 = "P:"
strRemotePath1 = "\\nowhere\nochance"
strRemotePath2 = "\\alan\drivers"

‘ Remove comment ‘ on next line
‘ On Error Resume Next

Set objNetwork = CreateObject("WScript.Network")
‘ Section which maps two drives, M: and P:
objNetwork.MapNetworkDrive strDriveLetter1, strRemotePath1

objNetwork.MapNetworkDrive strDriveLetter2, strRemotePath2

Wscript.Quit

‘ End of example VBScript

Learning Points

Note 1:  The whole point of our mission is run the script with, and then without, On Error Resume Next.  To compare the effects, uncomment line 17.

Note 2:  If in different circumstances, you wish to turn off On Error Resume Next, the command is:
On Error Goto 0.  Again be careful, GoTo is all one word, and the final character is a zero not an oh.

Note 3: The secret of using On Error Resume Next is to place it as near as possible to the problem section, and certainly after you have declared your variables.

Note 4: There are a surprising number of traps when mapping multiple drives.  The central point is that each new drive letter has its own line.
objNetwork.MapNetworkDrive strDriveLetter2, strRemotePath2.

Note 5: From a scripting point of view, just create one object.  It is not necessary to Set objNetwork1, objNetwork2 for each drive, just recycle the one variable – objNetwork.

This Week’s Second Mission – Run Explorer to confirm drive mapping.

This is a tricky mission.  Take extra care to read and study all that I am trying to achieve.  In truth this script contains multiple themes.  Naturally, it builds on the first mission: On Error Resume Next.  However, the second mission’s key component is
objShell.run ("Explorer" &" " & strDriveExplore & "\" ).  As I mentioned earlier, normally I would confirm success with a WScript.echo message, but here I have chosen to launch explorer at the very drive that we have just mapped.

To add spice, I have introduced a sub routine, which recycles code, for example, where you have multiple drives to map.  See Sub Explorer().

At the end of the script I added a RemoveNetworkDrive statement, but with a delay of 20 seconds (Sleep 20000), this was because I was fed up of manually disconnecting and thought it would be fun to leave it for you to see the action.

‘ Explorer.vbs – Map open explorer and map Drive to M: and P:
‘ VBScript to Demonstrate On Error Resume Next
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 2.6 – April 24th 2005
‘ ——————————————————–‘
Option Explicit
Dim objNetwork, objShell, strRemotePath1, strRemotePath2
Dim strDriveLetter1, strDriveLetter2, strDriveExplore

strDriveLetter1 = "M:"
strDriveLetter2 = "P:"
strRemotePath1 = "\\nowhere\nochance"
strRemotePath2 = "\\alan\drivers"

On Error Resume Next

Set objNetwork = CreateObject("WScript.Network")
‘ Section which maps two drives, M: and P:
objNetwork.MapNetworkDrive strDriveLetter1, strRemotePath1
strDriveExplore = strDriveLetter1
call Explorer()
objNetwork.MapNetworkDrive strDriveLetter2, strRemotePath2
strDriveExplore = strDriveLetter2
call Explorer()

Sub Explorer()
If err.number = vbEmpty then
Set objShell = CreateObject("WScript.Shell")
objShell.run ("Explorer" &" " & strDriveExplore & "\" )
Else Wscript.echo "VBScript Error: " & err.number
End If
err.clear
End Sub

‘ Removes Network drive after 20 seconds
WScript.Sleep 20000
objNetwork.RemoveNetworkDrive strDriveExplore
Wscript.Quit

‘ End of example VBScript

Learning Points

Note 1:  CreateObject("WScript.Shell") is an object that we can use to run explorer.  It contrasts with the Network Object that most VBScript commands use.

Note 2:  Observe how I declare, then employ: strDriveExplore. Follow the logic of how objShell runs the explorer and also how I concatenate a slash to persuade it to display the correct drive letter.

Summary – On Error Resume Next

If you are in a hurry but your script keeps throwing up WSH error messages, then try On Error Resume Next. If you have more time, then there are extras that you can add to your scripts, for example: sub routines, confirmation tactics and commands to rollback the script.

See more about VBScript error correcting codes

VBScripts  • 800 Code list  • Ezines  • Logon Scripts   • PowerShell ErrorAction   • Free Log Tool

Ezine 7 On Error Resume  •Ezine 15 Error Codes  • Ezine 33 Troubleshooting  • Free WMI Monitor

• Ezine 49 Errors  • Ezine 54 Error correcting  • Ezine 69 Option Explicit  • Ezine 71 On Error