Learn to use the WshNetwork object and the Select Case statement
In "Scripting Windows Management," April 2002, InstantDoc ID 24231, I introduced some basic scripting concepts. This month, I apply those concepts in a script that connects a user to network resources according to the user's account logon name. To make this script work, you need a way of representing network resources, a way of determining who's currently logged on, and a way of allocating the network resources according to who's logged on so that the resources are available to only the appropriate people. To accomplish these tasks, you can use VBScript and exploit a Windows Script Host (WSH) object.
Naming Network Resources
As you might recall from last month, an object represents a manageable item in the OS or file system. Objects must be exposed; that is, some part of the OS or file system has to make them available to the script. WSH abstracts the interface of many Win32 objectsthat is, WSH is both an object and a collection of subordinate objects. One of the objects in the WSH collection is WshNetwork (aka WScript.Network), a collection of network resources, shared drives, and printers. WshNetwork's methods take the Universal Naming Convention (UNC) paths to shared network resources as argumentsin other words, you can use the methods to create connections to network resources that you name. For a complete list of WshNetwork's methods and properties, see Table 1, page 82.
You can't address WshNetwork directly. To use it, you need to create a reference to it; you do so with the CreateObject function, supplying the WScript.Network name of the object as an argument. To create a reference to this network object and assign it to a variable so that you can work with it more easily, type
Set oNet = CreateObject
("WScript.Network")
(Note that I need to break this line and the other code snippets in this article for print publication purposes, but each statement, except Select Case, should be on one line.) The letter o that begins the variable name indicates that the variable represents an object.
Now that an oNet variable is representing the WshNetwork object, you can call on the object's properties and methods. To map network drives, you use a method called MapNetworkDrive. You can supply a drive letter and a network path as arguments to this method, as in
oNet.MapNetworkDrive "X:",
"\\servername\sharename"
to map the network resource to the drive. If the drive letter is already in use, you'll get an errorone reason to use letters from the end of the alphabet. Using the WshNetwork object to map a network drive makes a nonpersistent connection to the network path, which is handy for shared computers. If you log on, use the MapNetworkDrive method, then log off, any network mappings you created will be gone when someone else logs on with a different account name. You can use the other methods listed in Table 1 to programmatically add printers, list network resources, or unmap network resources from a computer.
Identifying the User Account
You now have a way to connect to the shared network resource. To get the user account information, you can use the same WshNetwork object you created above, represented by the oNet variable. However, this time, you call the object's UserName property, whose value is the logon name of the person currently logged on to the computer. For example, if I included the line
Wscript.Echo "The currently
logged on user is",
oNet.UserName
in a script and ran that script on a computer on which I was working, the script would display the message The currently logged on user is ChristaA. The WshNetwork object has two other properties: UserDomain, which returns the computer's domain name (or the computer's name if the computer isn't part of a domain), and ComputerName, which returns the computer's name.
Mapping Network Resources According to Identity
I've shown you how to use the WshNetwork object to map to network resources and identify the person who's logged on, but how do you make the script map to different network drives depending on who's logged on? That task calls for the VBScript Select Case statement. Select Case is a conditional statement that performs different actions depending on the value of a variable. You feed the statement a variable to test and provide instructions for each possible variable value.