Windows Logon Scripts - On Error Resume Next
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.
Topics for 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
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.
Instructions for multiple Mapped Network Drives
' MapTwoDrives.vbs - Map Network Drive to M: and P:
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.
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.
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. Give this permissions monitor a try - it's free!
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.
If you like this page then please share it with your friends
See more logon scripts examples