Introduction to Map Network Drive – Error Correcting Code
Once your MapNetworkDrive script is working, you may wish to add error correcting code. This page shows you strategies, tactics and methods to handle Microsoft VBScript problems, for example, the logon script fails because the servername is misspelt.
Topics for MapNetworkDrive Error Correcting Code
Your first decision is whether to simply ignore the script problem, whether it would be better to fix it. By ignoring the problem I mean that you could just add On Error Resume Next. Assuming you want to handle the problem you need to trap the WSH code number with err.number.
Error Number – Err.Number
Each Microsoft VBScript error has a number. What is confusing in troubleshooting is that sometimes the number is represented in hex, while on other occasions you see it in decimal. The WSH Code Error message boxes display in hex with a number beginning 800xxxxx. However, the err.number command will display the same error buy translated into decimal.
Each Windows Script Host error box has a wealth of information. Let us play detective and see what clues we can extract from the message and so solve the VBScript crime. The line number is probably the most useful clue as it pinpoint the area where the script errors. When you count lines, include blank lines and comments. The Char or character number along the line may, or may not be useful. Let me explain. If the number is greater than one then it’s significant, but if Char: 1, then it is VBScripts way of saying, there is something wrong on this line but I am not sure where.
In the example opposite, Source WSHNetwork.MapNetworkDrive confirms the object and method that are causing the error. Finally, the code hex number : 8000704CF, while having no meaning itself is invaluable in looking up similar errors in Google. My idea would be to research, and discover other people who have experienced the error and look for similarities between their cause and yours.
The line with Error: ‘The network location cannot be reached….’ yields more valuable information. What this means is that VBScript cannot find the server.
What Can you Do?
The burning question is what can you do about these error numbers? How will you handle them. To answer that question you need a mixture of pure scripting techniques, mixed with a little logic. Take as an example, the drive letter is already mapped, what would like the script to do?
a) Remove the mapped drive and map a different share.
b) Ignore mapping that drive letter, and proceed with the rest of the script.
A quick refresher of extra scripting terms needed for error handling.
Err.number – Each time WSH encounters an error, it displays a message box and code number and other useful details. Err is a valid VBScript object on a par with objNetwork. .number is its property, and the .number value is decimal not hex.
vbEmpty – Visual Basic empty, another way of saying zero. Once you get used to the term, vbEmpty is clearer than a 0, which could be a zero or the letter ‘oh’.
If .. then else. endif – This old faithful introduces branching and matches real life logic with scripting logic. If the drive is already mapped, then disconnect it.
Select Case – Expands the If…then, and especially the else. If you add more than 3 elseif statements the script gets messa, Select Case is an eloquent solution.
sub GuyCase() – Sub routine to call Select Case. I find you get more information if you call a subroutine after each drive mapping, otherwise you may miss a second or third error.
RemoveNetworkDrive – The opposite of MapNetworkDrive. A simple, easy to use VBScript command, common in logon scripts.
EnumNetworkDrives – Needed to count or enumerate the drives that already present. My view is EnumNetworkDrives gives us a handle so we can manipulate drives, for example disconnect them.
I like thePermissions 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!
Pre-requisites for Err.Number
- On Line 12 change the server name from ‘\\alan’ to your server name.
- Make sure that your server has a share called ‘\drivers’. Or else change the reference to an actual share on your server.
- Make a deliberate mistake with the name of the server on line 11.
- Copy and paste the script below into notepad.
- Change the server name from "\\alan to the name of your server.
- Make sure that one of your servernames does not exist.
- Save the file with .vbs extension e.g. OnErrResNext.vbs
- Double click the logon script and check in your Windows Explorer for two new drives called : drivers on ‘alan’ (Q:) and download reports on ‘alan’ (V:).
‘ OnErrResNext.vbs – Map Network Drive to Q: and V:
‘ VBScript to test On Error Resume Next.
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 1.2- April 24th 2010
Dim objNetwork, strRemotePath1, strRemotePath2
Dim strDriveLetter1, strDriveLetter2
strDriveLetter1 = "Q:"
strDriveLetter2 = "V:"
strRemotePath1 = "\\otherServer\data"
strRemotePath2 = "\\alan\drivers"
Set objNetwork = CreateObject("WScript.Network")
‘ Here is the one line error correcting code
On Error Resume Next
‘ Section which maps two drives, Q: and V:
objNetwork.MapNetworkDrive strDriveLetter1, strRemotePath1
objNetwork.MapNetworkDrive strDriveLetter2, strRemotePath2
‘ Extra code just to add a message box
WScript.Echo "Map drives? " & strDriveLetter1 & " & " & strDriveLetter2
‘ End of Script
Note 1: Try the script with, and without, line 15, On Error Resume Next.
Error Correcting Code – Summary
If you really want to master VBScript, then seek out methods, they are the ‘doers’, the verbs. To really get to know each method, research all the Error Correcting Code. One day an optional argument will solve a problem, for example permissions errors with MapNetworkDrive.
If you like this page then please share it with your friends