Is your server running slowly? Check with SolarWinds ipMonitor
Get a free evaluation copy of ipMonitor
Contents for Ezine 71 On Error Resume Next
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.
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.
If you are looking for handy network utilities, try some of the free downloads at
Tools4Ever
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 - On Line 14 change the server name from '\\alan' to your server name.
- 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 - Copy and paste the script below into notepad.
- Did you edit the server name from "\\alan to the name of your server?
- Make sure that you have one, network share on your server.
- Crucially, leave in the deliberate mistake \\nowhere\nochance
- Save the file with a .vbs extension e.g. TwoDrives.vbs.
- Double click the script and expect to see a Windows Script Host error message box.
- 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
http://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 PointsNote 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 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 http://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 PointsNote 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.
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.
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.
|