Introduction to Map Network Drive - UserName
This page has a specific goal, to map a network drive to a UserName, rather than the root of the \home share.
In many ways this is the classic logon script task, each user has an individual folder on the Window 2003 server, and you want the script to map to a different folder for each user.
I recommend that you master the simple MapNetworkDrive script, then graduate to this UserName VBScript. On the other hand, if you want a script to
actually create the user's folder, then check here.
Topics for MapNetworkDrive UserName
Suppose you want users to have their J:\ (home drive) mapped, not to the root share, but to a sub directory corresponding to their individual UserName. Here is how you script MapNetworkDrive. Instead of
connecting to Windows Server 2003 here: \\ server\home You prefer to MapNetworkDrive to a subfolder which is one level further down: \\ server\home\UserName Another possible scenario is where you want to map to subfolder on a normal, non-home directory share. For example, instead of connecting to a Windows server here: \\ BigServer\reports You prefer to MapNetworkDrive to a subfolder one level further down: \\
BigServer\reports\2005
Our objective is to map the
J: to a folder called '\home\UserName' on a server called '\\grand'.
My advice is to build up your logon scripts gradually, you may consider getting the simple MapNetworkDrive script working first. In my example opposite, the user's name is guyt, hence you see in
Windows Explorer: guyt on 'grand\home'(J:). Pre-requisites
- On Line 15 change the server name from '\\grand' to your server name.
- Make sure that your server has a share called '\home'.
- Underneath '\home', you have already created a folder which matches the name of the user you are testing. Moreover, the name of
this folder matches exactly, the users logon name.
Instructions to MapNetworkDrive - Copy and paste the script below into notepad.
- Change the server name from "\\grand to the name of your server.
- Save the file with .vbs extension e.g. MNDUserName.vbs.
- Double click your logon script and check in your Windows Explorer for a new drive called : UserName on 'grand' (J:)
' MNDUserName.vbs ' VBScript to map a network drive to the UserName. ' Author Guy Thomas http://computerperformance.co.uk/ ' Version 1.3 - April 24th 2005 '
-----------------------------------------------------------------' Option Explicit Dim objNetwork Dim strDriveLetter, strRemotePath, strUserName strDriveLetter = "J:" strRemotePath =
"\\grand\home"
' Purpose of script to create a network object. (objNetwork) ' Then to apply the MapNetworkDrive method. Result J: drive Set objNetwork = WScript.CreateObject("WScript.Network")
' Here is where we extract the UserName strUserName = objNetwork.UserName objNetwork.MapNetworkDrive strDriveLetter, strRemotePath _ & "\" & strUserName
' Extra code just to add a message
box WScript.Echo " Launch Explorer, check: "& strDriveLetter WScript.Quit
' End of MapNetworkDrive Example logon script.
Learning PointsNote 1: The script proper begins by creating a network object thus: Set objNetwork = WScript.CreateObject("WScript.Network"). This emphasises the role of the
WScript in creating and hosting the object, which go on to manipulate the network drive. In fact the first WScript is optional, even redundant this works just as well: Set objNetwork =
CreateObject("WScript.Network"). To me these tiny differences give perspective and appreciation to what the command is achieving. Note 2: As line 17 was so long, I needed the _ (underscore), which joins the two lines when VBScript processes the command. Without the underscore the code does
not work, VBScript believes you have two lines because it does not understand word-wrap.
Note 3: See how the script cleverly joins, or concatenates the strUserName to the strRemotePath. (My) VBScripts use lots of & (ampersands) to
concatenate variables. Note 4: The tiniest but most troublesome part of this script is the slash "\" after home. Always remember that if your mapping
needs extra subfolders, then put in another "\" between each folder level. In scripting terms this is a small point, but in practical terms it's so vital that I have another example showing a different
way of achieving the same goal, namely to map to the UserName folder.
Note: If you want a script which will actually create the home folders, check here.
This logon script does exactly the same job as Example 1. What I want to highlight here is the
importance of attention to detail, in particular constructing the path to the user's folder using '\'.
' MNDUserName2.vbs ' VBScript to map a network drive to the UserName. ' Author Guy Thomas http://computerperformance.co.uk/ ' Version 2.1 - April 24th 2005 '
-----------------------------------------------------------------' Option Explicit Dim objNetwork Dim strDriveLetter, strRemotePath, strUserName strDriveLetter = "K:" strRemotePath =
"\\grand\home\"
' Purpose of the script to create a network object. (objNetwork) ' Then to apply the MapNetworkDrive method. Result K: drive Set objNetwork = WScript.CreateObject("WScript.Network")
' Here is where we extract the UserName strUserName = objNetwork.UserName objNetwork.MapNetworkDrive strDriveLetter, strRemotePath & strUserName
' Extra code just to add a message box
WScript.Echo " Launch Windows Explorer, check: "& strDriveLetter WScript.Quit
' End of Example of MapNetworkDrive VBScript.
VBS Learning Points
Note 1: Can you see how I control the troublesome "\" by using:
strRemotePath = "\\grand\home\" (Not strRemotePath = "\\grand\home" ) If harping on about the slash prevents you making a mistake, then I will
feel justified in showing you this second method.
Note 2: I deliberately changed the drive letter from J: to K: so that it would not interfere with the Example 1 logon script. Note: If you want a script which will actually create the home folders, check here.
Here is a bonus script. It introduces two features, a sub routine, and instead of a message box, objShell creates an instance of Windows Explorer to display the mapped network drive.
Naturally, this sub routine is not needed in a production script. Pre-requisites.
- On Line 10 change the server name from '\\grand' to your server name.
- Make sure that your server has a share called '\home'.
Instructions to MapNetworkDrive - Copy and paste the script below into notepad.
- Change the server name from "\\grand to the name of your server.
- Save the file with .vbs extension e.g. MNDUserNameBonus.vbs.
- Double click and check observe your Windows Explorer open the new mapped X: drive.
' MNDUserNameBonus.vbs ' VBScript to map a network drive to the UserName. And open Explorer ' Author Guy Thomas http://computerperformance.co.uk/ ' Version 2.2 - September 2005
' -----------------------------------------------------------------' Option Explicit Dim objNetwork, objShell Dim strDriveLetter, strRemotePath, strUserName strDriveLetter = "X:" strRemotePath
= "\\grand\home\"
' Purpose of the script to create a network object. (objNetwork) ' Then to apply the MapNetworkDrive method. Result K: drive Set objNetwork =
WScript.CreateObject("WScript.Network") ' Here is where we extract the UserName strUserName = objNetwork.UserName objNetwork.MapNetworkDrive strDriveLetter, strRemotePath & strUserName
' Bonus
code to open Explorer at the mapped drive letter Call ShowExplorer WScript.Quit
Sub ShowExplorer() Set objShell = CreateObject("WScript.Shell") objShell.run ("Explorer" & " " &
strDriveLetter & "\" ) End Sub
' End of Example of MapNetworkDrive logon script.
Learning Points
Note 1: Observe the Sub ShowExplorer(). As a change from a WScript.Echo, here is a script which finishes by opening Windows Explorer. While the sub routine is at the very end of
the script it is accessed via the Call ShowExplorer earlier in the script.
Being able to MapNetworkDrive to a subfolder extends the capability of Microsoft's VBScript. The secret is to understand where to place the extra slash '\' which tells the script where find the subfolders. Another important
scripting principle, is to master the basic logon script before attempting this more difficult example of multiple map network drives.
See Also● Logon Script Home ● Create Users Home Folders ●
MapNetworkDrive Arguments ● RemoveNetworkDrive ●
EnumNetworkDrives
|