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
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.
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:
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.
SolarWinds' Orion performance monitor will help you discover what's happening on your network. This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.
What I like best is the way NPM suggests solutions to network problems. Its also has the ability to monitor the health of individual VMware virtual machines. If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.
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.
Instructions to MapNetworkDrive
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.
strRemotePath, false, _
NTM will produce a neat diagram of your network topology. But that's just the start; Network Topology Mapper 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 test drive the Network Topology Mapper then you will find a device on your network that you had forgotten about, or someone else installed without you realizing!
Download your 14 day free trial of SolarWinds Network Topology Mapper
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.