Contents of Guy’s Scripting Ezine 7 – On error resume next
- This Week’s Secret
- Example 1 – Designed to fail with error message
- Example 2 – Half the script works
- Example 3 – On error resume next
- Troubleshooting and Resources
This Week’s Secret
I will let you into a secret, sometimes you need to know a little theory. So before we get on with this week scripts, here is some background information. VBScript is an interpreted language; whereas other languages like Perl and C++ are compiled first then executed. I hear you saying, ‘So what. Who cares?’ Well, how the scripting language behaves matters when you are troubleshooting.
When I first I saw a VBScript error message, I was surprised, not because there was a mistake, but because part of the script had actually worked. I had wrongly expected that the WSH engine would abort the whole code. My message is this: watch out when you are experimenting, a faulty script may work up to the error and then the second part fails.
To illustrate this point I would like you to experiment with the classic map network drive script.Firstly let us get in synch, please check these three learning goals:
- To troubleshoot VBScript
- To understand what happens when you get an error message
- To decide whether to use: ‘on error resume next’
Secondly preparing and editing my scripts is particularly important this week.
When you examine the three Example scripts:
a) Substitute one of your real servers and shares for \\alan\home
b) Leave \\offline\home as it is – this is the deliberate error.
Guy Recommends: The Free IP Address Tracker (IPAT)
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
Example 1: A script designed to fail with an error message
Note: This Example is designed to fail! The result should be no drives get mapped and you get an error message.
‘ VBScript.
‘ Purpose of script to map a network drive to Y:\
‘ Substitute \\alan\home
‘ Deliberate error leave \\offline\home to generate an error
Set WshShell = WScript.CreateObject("WScript.Shell")
Set Network1 = WScript.CreateObject("WScript.Network")
Set Network2 = WScript.CreateObject("WScript.Network")
DriveLetter1 = "W:"
DriveLetter2 = "Y:"
RemotePath1 = "\\offline\home"
RemotePath2 = "\\alan\home"
Network1.MapNetworkDrive DriveLetter1, RemotePath1
Network2.MapNetworkDrive DriveLetter2, RemotePath2
‘ END of script
Troubleshooting: Try this reference
Example 2: A script designed to partially work with an error message
Warning – disconnect the network drive that you created in Example1.
Note: This Example is designed to HALF work and generate error message – 800704B3. The difference here is the order of Network2 and Network1. I have also changed the order of RemotePath2 and RemotePath1 to emphasise the difference.
Double check that you have altered \\alan\home to a real server and share, leave \\offline\home to generate the error.
‘ VBScript.
‘ Purpose of script to map a network drive to Y:\
‘ Substitute \\alan\home
‘ Deliberate error leave \\offline\home to generate an error
Set WshShell = WScript.CreateObject("WScript.Shell")
Set Network1 = WScript.CreateObject("WScript.Network")
Set Network2 = WScript.CreateObject("WScript.Network")
DriveLetter1 = "W:"
DriveLetter2 = "Y:"
RemotePath2 = "\\alan\home"
RemotePath1 = "\\offline\home"
Network2.MapNetworkDrive DriveLetter2, RemotePath2
Network1.MapNetworkDrive DriveLetter1, RemotePath1
‘ END of script
Troubleshooting: Try this reference
Try disconnecting the Y: drive from the previous example.
Example 3: Try – On error resume next
Warning – disconnect the network drive that you created in Example1.
Note this Example is designed to map the SECOND network drive and give no error message because of line 6: on error resume next
‘ VBScript.
‘ Purpose of script to map a network drive to Y:\
‘ Substitute \\alan\home
‘ Deliberate error leave \\offline\home to generate an error
‘ Note the next line. It suppresses error messages and carries on.
on error resume next
Set WshShell = WScript.CreateObject("WScript.Shell")
Set Network1 = WScript.CreateObject("WScript.Network")
Set Network2 = WScript.CreateObject("WScript.Network")
DriveLetter1 = "W:"
DriveLetter2 = "Y:"
RemotePath2 = "\\alan\home"
RemotePath1 = "\\offline\home"
Network2.MapNetworkDrive DriveLetter2, RemotePath2
Network1.MapNetworkDrive DriveLetter1, RemotePath1
‘ END of script
Troubleshooting Resources
- Try disconnecting the Y: drive from the previous example.
- Troubleshooting: Try this reference.
- Feeling adventurous? Try incorporating RemoveNetworkDrive method in your test scripts.
- For more information try Logon Script Home
- or Try WSH Home
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