Our login scripts contained lots of net use statements in *.BAT or *.CMD files. The command looked like this:
NET USE H: //server/share/directory1/directory2/directory3
and it would show in explorer like this:
directory3 on //server/share/directory1/directory2 (H:)
My goal was to make a script just as easy to use at NET USE. That is exactly what I ended up with. The new command looks like this:
Map.vbs H: //server/share/directory1/directory2/directory3 "My directory3"
and it would map like this:
My directory3 (H:)
By making the command simple the scripts are easy to read and modify by others on my team. Now that you know what we are doing, lets take a look at the code needed to make this work.
We are going to save this code with the name Map.vbs. Its a VBScript file executed by the windows scripting host. To make the script more usable, it needs to read the variables from the command line. The first 3 lines populate the variables we will use later.
strDriveLetter = WScript.Arguments(0)
strPath = WScript.Arguments(1)
strDisplayName = WScript.Arguments(2)
The next 2 lines I am going to show you display how to map the drive.
Set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive strDriveLetter, strPath, true
This will map the path strPath to the drive strDriveLetter. The true as the 3rd param is the same as using the /PERSIST param of NET USE. It saves the mapping in the profile.
Now we need to change the name of the mapping. This takes an existing share by drive letter and gives it a custom display name. This was one of the hardest pieces of code to find. The next 2 lines do just that.
Set oShell = CreateObject("Shell.Application")
oShell.NameSpace(strDriveLetter & "\").Self.Name = strDisplayName
If you put that all together you have a bare bones script to map a drive and rename it from the command line via a VBScript. In my next post, I plan on going more in depth into my map.vbs. I will show you how to check for an existing mapping on that letter and all the validation I added.