Logon Scripts

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

 ♦

Introduction to 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.

Example of  On Error Resume Next

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

  1. On Line 14 change the server name from '\\BigServer' to your server name.
  2. 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

  1. Copy and paste the script below into notepad or get a script editor such as OnScript (free download).
  2. Did you edit the server name from "\\BigServer to the name of your server?
  3. Make sure that you have a network share called drivers on your server.
  4. Crucially, leave in the deliberate mistake \\nowhere\nochance
  5. Save the file with a .vbs extension e.g. MapTwoDrives.vbs.
  6. Double click the VBScript file and expect to see a Windows Script Host error message box.
  7. 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

strDriveLetter1 = "M:"
strDriveLetter2 = "P:"
strRemotePath1 = "\\nowhere\nochance"
strRemotePath2 = "\\BigServer\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 Script

Learning Points

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 ToolFree Permissions Analyzer for Active Directory

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!

Download Permissions Analyser - Free Active Directory Tool

Background of Error Correcting Code

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 - Summary

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.

 

Download my Logon Script eBook for only $6.25

Logon ScriptThe 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.

  Jumbo Script 7 Package

If you like this page then please share it with your friends

 


See more logon scripts examples

Logon Script Home   • EnumNetworkDrives   • Map Network Drive Group   • Free Import CSV Tool

Map Network Drive Script   • Vbscript Map Network Drive Username   • Map Multiple Network Drives

ObjNetwork.MapNetworkDrive   • Disconnect Network Drives   • Logon script group policy

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor for PowershellSolarwinds WMI Monitor

Windows Management Instrumentation (WMI) is most useful for PowerShell scripting.

SolarWinds have produced this Free WMI Monitor to take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2014 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: