Introduction to Windows Logon Scripts - On Error Resume Next
Our goal is to create defensive code that protects the user against the most common logon script problems.
Let us start with a simple strategy. We are going to tell the script not to give up, but to ignore the error and carry on with the next instruction. The VBScript command to achieve our goal is
: On Error Resume Next.
When people first discover On Error Resume Next, the temptation is to use it all the time in their
Windows logon scripts. So, the second lesson is to use On Error Resume Next only sparingly. For example, you positively need to know if you forgot to declare a variable. Therefore, don't
use On Error Resume Next in the header section, and just turn it on for sections of code which benefit from ignoring errors. When you have finished return to normal error reporting with On Error Goto 0. Just to be clear, goto is all one word and that's a zero not an oh.
To test On Error Resume Next, I have an example where a
Windows logon script seeks to map not one, but two Network Drives. If, for any reason the script fails to map the first drive, we want to say, forget that error and try and map the second drive.
Mapping one drive is better than none, and certainly better than an error message interrupting the user.
Pre-requisites
On Line 14 change the server name from '\\BigServer' 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 or get a script editor such as OnScript (free download).
Did you edit the server name from "\\BigServer to the name of your server?
Make sure that you have a network share called drivers on your server.
Crucially, leave in the deliberate mistake \\nowhere\nochance
Save the file with a .vbs extension e.g. MapTwoDrives.vbs.
Double click the VBScript file 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.
' MapTwoDrives.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 2010 ' --------------------------------------------------------' Option Explicit Dim objNetwork, objShell, strRemotePath1,
strRemotePath2 Dim strDriveLetter1, strDriveLetter2, strDriveExplore
Note 1: The point of this script is to run it first with, and then without, On Error Resume Next. To compare these two effects, uncomment line 17.
Note 2: 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 3: 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 4: From a scripting point of view, all you need is one object. It is not necessary to Set objNetwork1, objNetwork2 for each drive, just recycle the one variable - objNetwork.
Guy
Recommends: Permissions Analyzer - Free Active Directory Tool
I like the
Permissions Monitor because it enables me to see quickly WHO has permissions
to do WHAT. When you launch this tool it analyzes a users effective NTFS
permissions for a specific file or folder, takes into account network share
access, then displays the results in a nifty desktop dashboard!
Think of all the frustration that this free utility saves when you are
troubleshooting authorization problems for users access to a resource.
What can you do to prevent 800xxxxx error messages assaulting your scripts? The answer is to defend with error correcting code. At its best, error correcting code can
build in logic. For example, suppose a logon script fails because the drive letter is already in use. How can you defend against this error? Well if it's a Network Drive then you could
disconnect,
or else try another letter. A major part of writing error correcting code is anticipating what could wrong with the script. Once you realize the outcome, then visualize the
manual steps you would take to get the task back on track. Finally write the code to mimic those manual steps.
On Error Resume Next is a wonderful addition to your Window logon script toolkit. Add this command when ever your want to ignore a 800xxxxx error and carry on processing the rest of your script.
The extra features you get in your eBook
include, more pages full of detailed examples. Also, ten 'how to...'
sections, with screen shots showing which menus to use. Go for Guy's
eBook - and get a printable version with copy enabled and no expiry date.
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems.
Fortunately, Solarwinds
have created the
Free WMI Monitor so that you can actually see and understand these gems of
performance information. Take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.