Logon Scripts

VBS Logon Script - MapNetworkDrive

Introduction to MapNetworkDrive for 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

Example 1 - MapNetworkDrive - Basic

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.

Pre-requisites

  1. You need a computer with a network share.   Therefore go to Line 2 of my script and change the name of the server from '\\alan' to the name of your server.
  2. Either make sure that your shared folder is called '\home', or alternatively, alter the word '\home' in the script to match the name of your UNC share.

 

Instructions to MapNetworkDrive

  1. Copy and paste the script below into notepad or get a script editor such as OnScript (free download).
  2. Change the server name from "\\alan to the name of your server.
  3. Check the share name '\home'.
  4. Save the file with a .vbs extension, e.g. MND.vbs or MapNetworkDrive.vbs.
  5. Double click the resulting script and look in your Windows Explorer for a new drive letter called : home on 'alan' (H:)

 

Set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive "H:" , "\\alan\home"

Learning Points

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.MapNetworkDrive VBScript map network drive

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.

̃

Example 2 - MapNetworkDrive with extra VBScript code

Our objective remains to map a drive, but this time the J:.  My share name and server are the same as example 1, '\home' and '\\alan'.

Pre-requisites.

  1. On Line 10 change the server name from '\\alan' to your server name.
  2. Make sure that your server has a share called '\home'.

 

Instructions to MapNetworkDrive

  1. Copy and paste the script below into notepad.
  2. Check strPath, your server is unlikely to be called "\\alan, so amend to the name of your server.
  3. Save the file with .vbs extension e.g. MapNetworkDrive.vbs.
  4. Double click your script and check in your Windows Explorer for a new drive called : home on 'alan' (J:)

' MapNetworkDrive.vbs
' VBScript to map a network drive to a UNC Path.
' Author Guy Thomas http://computerperformance.co.uk/
' Version 2.3 - September 2005
' -----------------------------------------------------------------'
Option Explicit
Dim objNetwork
Dim strDriveLetter, strRemotePath
strDriveLetter = "J:"
strRemotePath = "\\alan\home"

' Purpose of script to create a network object. (objNetwork)
' Then to apply the MapNetworkDrive method.  Result J: drive
Set objNetwork = CreateObject("WScript.Network")

objNetwork.MapNetworkDrive strDriveLetter, strRemotePath
WScript.Quit

' End of Example VBScript.

Learning Points

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.

Help for error code 80070055 and similar WSH Messages

Example 3 - MapNetworkDrive with Message box

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.

Pre-requisites.

  1. On Line 10 change the server name from '\\alan' to your server name.
  2. Make sure that your server has a share called '\home'.

 

Instructions to MapNetworkDrive

  1. Copy and paste the script below into notepad.
  2. Change the server name from "\\alan to the name of your server.
  3. Save the file with .vbs extension e.g. MapNetworkDrive.vbs.
  4. Double click and check in your Windows Explorer for a new drive called :
    home on 'alan' (K:)

 

' MapNetworkDrive.vbs
' VBScript to map a network drive. And provide a message box
' Author Guy Thomas http://computerperformance.co.uk/
' Version 3.2 - September 2005
' -----------------------------------------------------------------'
Option Explicit
Dim objNetwork
Dim strDriveLetter, strRemotePath

strDriveLetter = "K:"
strRemotePath = "\\alan\home"

' Purpose of the script to create a network object. (objNetwork)
' Then to apply the MapNetworkDrive method. Result K: drive
Set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath

' Extra code just to add a message box
WScript.Echo " Launch Explorer, check: "& strDriveLetter

WScript.Quit

' End of MapNetworkDrive Example Logon Script.

 

Learning Points

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.

 Summary - MapNetworkDrive Logon Scripts

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.


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

See Also

Logon Script Home   MapNetworkDrive Arguments  ● RemoveNetworkDrive   EnumNetworkDrives

 *


Google

WebComputerperformance.co.uk

GFi Events Manager

Guy Recommends: GFi EventsManager

Here is a solution to monitor, manage and archive thousands of events that are generated by devices across your entire network.  Get your free evaluation copy of GFI EventsManager.

 

Home Copyright © 1999-2008 Computer Performance LTD All rights reserved

Please report a broken link, or an error.