Windows Logon Script - MapNetworkDrive to UserName
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 VBS 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:
You prefer to MapNetworkDrive to a subfolder which is one level further down:
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:
You prefer to MapNetworkDrive to a subfolder one level further down:
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
Instructions to MapNetworkDrive
Note 1: The script proper begins by creating a network object thus:
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.
Guy Recommends SolarWinds' Free Network Monitor
Thus utility makes it easy to check the health of a router or firewall. Check the real-time performance, and availability statistics, for any device on your network. Get started with an extensive collection of "out-of-the-box" monitors for popular network devices. Give Network Monitor a whirl - it's free. Download your free Network Device Monitor
If you need more comprehensive
network analysis software:
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 '\'.
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.
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!
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.
Instructions to MapNetworkDrive
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.
Employ PowerShell for Your Logon Scripts
Instead of VBScripts you could use PowerShell to create logon scripts. Although PowerShell is used mainly for configuring the operating system, it's possible to build ComObjects to MapNetworkDrive. The technique involves creating a wrapper around 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 PowerShell and logon scripts.
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.
If you like this page then please share it with your friends
See more logon scripts examples