Is your server running slowly? Check with SolarWinds ipMonitor
Get a free evaluation copy of ipMonitor
Contents of Guy's Scripting Ezine 7 - On error resume next
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.
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
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.
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
Their topics and material are ideal for getting you started with VBScript. The
videos are easy to follow and you can control the pace. Try their free demo material and then see if you want to buy the full package.
See more about VB Script Training CD.
|