VBS Logon Script - MapNetworkDrive
Introduction to MapNetworkDrive Logon Scripts
The whole theme of my site is getting you started. Therefore, here is a series of three VBScripts which map a network drive to a network share. There are few VBScript frills on this page, just VBScripts for you to learn how to map a network drive by copying and then amending my code.
Topics for Simple MapNetworkDrive VBScript
Our objective is to map the H: to a share called '\home'. In my example the server is called '\\alan', therefore the full UNC path is \\alan\home. We will be using Microsoft's VBScript and the code executes on any WSH client. The best environment would be Windows Server 2003 with an XP client. However, to practice MapNetworkDrive scripts, you could create a share on your own desktop computer.
Instructions to Create a Map Network Drive Script
' Map network drive script
Note 1: VBScript is an object based scripting language. On line 1 you can see how the code creates objNetwork. Line 2 then applies the famous MapNetworkDrive method to achieve the actual drive mapping.
Note 2: If you run the script a second time, then you get WSH error message code 80070055. The solution is launch Windows Explorer, and then just right-click on: home on 'alan' (H:) and select: Disconnect. (See diagram opposite). Now you can run the script again. Alternatively, you could create a fancy script, which maps to the first free drive letter, however that is difficult task, but see here if you want to try that advanced script.
Note 3: You could try mapping to a different drive letter, there is nothing magical about the H:\. MapNetworkDrive works just as well with the J:\ or Z:\.
Note 4: I reduced this first example script to the bare minimum. All I wanted was for you to gain success mapping a network drive. The next examples are more typical of production Microsoft VBScripts because they introduce variables, comments and a header.
Guy Recommends: The Free IP Address Tracker (IPAT)
Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets. IPAT cracks this problem of allocating IP addresses in networks in two ways:
For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges. For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers.
Our objective with this map network drive script remains to map a drive, but this time it's the J: drive. My share name and server are the same as example 1, '\home' and '\\alan'.
Instructions to MapNetworkDrive
Note 1: At the top of the script is a heading section. The idea of the header is to explain what this VBScript will achieve. Some script writers feel that the Dim statements, which declare variables, are also part of the header section.
Note 2: Option Explicit is a VBScript command which forces me to declare variables. Not only is this 'best practice', but in my case, it alerts me to typos later in the script.
Note 3: See how this script declares the variables strDriveLetter and strRemotePath, then reuses them later in the script. If you stick with me, you will see that I love variables. In this example, MapNetworkDrive employs just two arguments, drive letter and UNC path. See here for the full list of MapNetworkDrive arguments.
Note 4: Once we declare strDriveLetter, then we can assign it a value, in this case "J:". One perennial problem I have with scripting is paying attention to detail, especially the syntax. Even with a simple letter - J, we must be careful. For the script to succeed we need precisely "J:". Neither "J:\", nor "J\:" will work.
Note 5: All three examples on this page employ "Network" objects, as defined by CreateObject("WScript.Network"). I mention the Network object because in the broader picture, VBScript can also create other objects, for example, Shell or LDAP / 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!
Our objective remains to map a drive, but this time I have chosen the K: drive. My share name and server are the same as example 1, '\home' and '\\alan'. The only difference is to add a message box so that you know what's happening.
Instructions to MapNetworkDrive
strDriveLetter = "K:"
Note 1: Debugging is not one of Microsoft VBScript's strengths. This is why I include so many message boxes in my test scripts. Such messages are not necessary in production scripts, mainly because they confuse or annoy users.
Note 2: See how I cunningly reuse the variable strDriveLetter in the WScript.Echo. Perhaps you are beginning to realize why I am so keen on variables.
Use PowerShell for Logon Scripts
VBScripts are being superseded by PowerShell .ps1 files. While PowerShell is used mainly for configuring the operating system, it's possible to its cmdlets to MapNetworkDrive. The technique is to create a ComObject, which can act as a wrapper for familiar VBScript commands. Here is example of PowerShell's New-Object cmdlet manipulating MapNetworkDrive:
# PowerShell Logon Script Example
You could save these instructions in a .ps1 file. However, the hard part is executing this .ps1 file as a logon script. See more about assigning PowerShell logon scripts.
The modern group policy method of drive mapping does not require any knowledge of either VBScript or PowerShell. In Windows Server 2008 you can launch the GPMC and configure Drive Maps in the Preferences section. See more on Group Policy Drive Maps.
The mission of this page was to get you started creating logon scripts. Mapping a drive letter to a UNC is mainstream application of VBScript. I wanted to give you three variations of the MapNetworkDrive method, so that you could decide which features to include in your logon script.
If you like this page then please share it with your friends
See more logon scripts examples