Logon Scripts

Windows Logon Scripts - Five Arguments for MapNetworkDrive

Introduction to Map Network Drive - Five Arguments

Whilst keeping in mind our goal, which is to map a network drive, this page explains the optional arguments available to the MapNetworkDrive method.  In addition to the network path and local drive letter, you can also specify the Active Directory credentials that a user needs to access the network share.

Topics for MapNetworkDrive - Five Arguments

 ♦

VBScript Background

Microsoft designed VBScript on the classical object, method and value structure.  The idea is that you create an object and then apply methods such as MapNetworkDrive.  In turn, the method or verb, manipulates values. 

Take as an example, assigning a drive letter to a Windows network share.  The key command is MapNetworkDrive, however, it needs a network object before MapNetworkDrive can assign the drive letter to the share.  In my minds eye, WSH has a whole library of objects.  Our job is to persuade VBScript to release one of these objects any time the method needs to manipulate values.  In VBScript, creating an object is summed up in one neat line:

Set objNetwork = CreateObject("WScript.Network")

I chose to call the object 'objNetwork'.  You can call the object what you like, but scripters like to stick to a pattern with their variables.  A prefix of str indicates a string value, while an obj prefix denotes an object.  Now that WScript has created objNetwork, we can start manipulating it with the method MapNetworkDrive.

MapNetworkDrive Arguments

When ever you employ a VBScript method to manipulate an object, take the time to check its arguments and syntax.  When you create the network drive letter there may complications, for example permission problems.  To overcome such problems, methods have optional arguments, in this case strUser and strPassword. Here is the full list of arguments for MapNetworkDrive:

objNetwork.MapNetworkDrive:
1) strDriveLetter, 2) strRemotePath, 3) bUpdateProfile, 4) strUser, 5) strPassword.

For most scripts you will only need MapNetworkDrive's two compulsory arguments, the drive letter (1) and the network share (2).  In addition, there are three optional arguments.  There is a command to save the drive letter in the user's profile.  You can also add username and password arguments so that you could connect to the share with a more privileged user account.  For instance, if you wish to map to a share where the ordinary users only have read access the mapping will fail, so you to map with an account which has full control of the network share.  If you omitted this step then the script would fail because the user did not have sufficient permissions to complete the drive mapping.

Special note. The format for the username can be plain "Guythom" or "Administrator".  If this gives trouble, try adding the domain name e.g. "cpdom\Guythom" or "cpdom\Administrator".  Where cpdom is the domain name.  Actually, this tip is especially useful where you are trying to map to a member server, in which case the cpdom\ prefix would be the machine name.

A little more about the third argument, which is referred to as bUpdateProfile.  This optional component controls whether the new mapped network drive should be stored in the user's profile.  Incidentally it took me ages to discover what the 'b' in bUpdateProfile meant, the answer was Boolean, in other words this argument takes either true or false.  The default is false, so the only time you need to enter a value is if you positively want the user to receive this drive every time they logon, in which case you set the bUpdateProfile value to true.

In conclusion, while there are 5 arguments, most of the time you will only need two, strDriveLetter and strRemotePath.  I have to tell you that while you must use these to arguments correctly, you may name the corresponding variables what ever you like.  For instance, I declare strDriveLetter for the drive letter.  If you prefer, you could call this argument dLetter or LocalDrive, in your scripts.  My point is learn to recognise when a method name is fixed and when a variable name is flexible.  For example you cannot refer to the method MapNetworkDrive as MapEmGood, but you can call the my variable strRemotePath  strRmPth if you prefer.  Naturally, whether you call it strRemotePath or strRmPth, the server and sharename must exist and be correctly spelt.

Guy Recommends: SolarWinds LANSurveyorSolarwinds LANSurveyor

LANSurveyor will produce a neat diagram of your network topology.  But that's just the start; LANSurveyor can create an inventory of the hardware and software of your machines and network devices.  Other neat features include dynamic update for when you add new devices to your network.  I also love the ability to export the diagrams to Microsoft Visio.

Finally, Guy bets that if you take a free trial of LANSurveyor then you will find a device on your network that you had forgotten about, or someone else installed without you realizing!

Download a Free Trial of LANSurveyor

Example 1 - MapNetworkDrive with 5 Arguments

Our objective is to map the H: to a share called '\home' on a Windows 2003 server called '\\alan'.  However in this example I want to show you how to employ all 5 of the MapNetworkDrive arguments, strDriveLetter, strRemotePath, strProfile, strUser and strPassword.

Pre-requisites

  1. On Line 15 change the server name from '\\alan' to your server name.
  2. Make sure that the shared folder is called '\home'.  Alternatively, alter the word '\home' in the script if your share is called something else.

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 .vbs extension e.g. MNDArguments.vbs.
  5. Double click the resulting logon script and look in your Windows Explorer for a new drive letter: home on 'alan' (H:)


' MNDArguments.vbs
' VBScript to map a network drive with all 5 arguments.
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.3 - April 24th 2005
' ----------------------------------------------------------------------'
Option Explicit
Dim objNetwork
Dim strDriveLetter, strRemotePath, strUser, strPassword, strProfile

' Values of variables set
strDriveLetter = "H:"
strRemotePath = "\\alan\home"
strUser = "guytom"
strPassword = "£@ssw0rd1"
strProfile = "false"

' This section creates a network object. (objNetwork)
' Then apply MapNetworkDrive method. Result H: drive
' Note, this script features 5 arguments on lines 21/22.
Set objNetwork = WScript.CreateObject("WScript.Network")
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath, _
strProfile, strUser, strPassword

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

' End of Example script .

Learning Points

Note 1: If necessary, create a share on your own machine. I confess, that just to get the VBScript working, I often experiment on my own machine.

Note 2: If you run the script a second time, then you get WSH error message code 80070055.  The solution is launch Windows Explorer, just right click on: home on 'alan' (H:) and select: Disconnect.  See diagram opposite.  Now you can run the script again.

Note 3: If you run the script a second time you get WSH error message code 800704xx, check the name of your server.

Note 4: Where a line is too long and needs a break, use the _ (underscore) and then continue on the next line.  Microsoft's VBScript does not understand word-wrap so this is why you need underscore for statements that span two lines.

objNetwork.MapNetworkDrive strDriveLetter, strRemotePath, false, _
strUser, strPassword

Example 2 - MapNetworkDrive variations

This script does exactly the same job as Example 1.  What I want to demonstrate here is that there are a multitude of ways of scripting.  By approaching the problem for a different angle you get perspective on what Microsoft's VBScript is all about.

Notes

  1. This example uses lower case for strDriveLetter.  X or x would work equally well.
  2. Instead of declaring the optional arguments in advance, I just typed them in where needed.
 


' MNDArguments2.vbs
' VBScript to map a network drive with all 5 arguments.
' Author Guy Thomas http://computerperformance.co.uk/
' Version 2.2 - April 24th 2005
' -----------------------------------------------------------------'
Option Explicit
Dim objNetwork
Dim strDriveLetter, strRemotePath, strUser, strPassword, strProfile

' Values of variables set
strDriveLetter = "X:"
strRemotePath = "\\alan\home"

' This section creates a network object. (objNetwork)
' Then to apply the MapNetworkDrive method. Result X: drive
Set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath, false, gt, pass

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

' End of Example VBScript.

Learning Points

Note 1: Try and figure out what you can and must change, e.g. servername.  Also, what is a special word cannot be varied in any way, e.g. WScript.Network.

Note 2: I shortened Set objNetwork = WScript.CreateObject("WScript.Network") by removing WScript
Set objNetwork = CreateObject("WScript.Network").  Note: you cannot shorten WScript.Network to plain Network.

MapNetworkDrive Arguments Summary

If you want to master Microsoft's VBScript, then seek out methods, they are the 'doers', the verbs.  To really get to know each method, research all the arguments.  One day an optional argument will solve a problem, for example permissions errors with MapNetworkDrive.


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  Multiple Network Drives  Rename a Network Drive

 *


Google

Web  This website

Review of Orion NPMGuy Recommends: Orion's Network Performance Monitor (NPM)

Orion NPM is designed for detecting network outages.

Network-centric views (screenshot) make it easy to see what's working, and what needs your attention.

Download your free trial of Orion's network performance monitor

 

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

Please report a broken link, or an error.