Skip navigation

Connecting Users to Network Resources

Downloads
24893.zip

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 objects—that 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 arguments—in 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 error—one 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.

If only a few people need special network mappings, you can list those exceptions, then use Select Case's Case Else option to spell out the action to take for everyone else. You end Select Case with an End Select statement. The syntax for Select Case is

Select Case variable
   Case A
       statements for A
   Case B
       statements for B
   Case C
       statements for C
   Case Else
       statements for Else
End Select

where variable contains the value you want to test against the various cases (e.g., Case A, Case B) and statements for A, statements for B, and so on spell out the action to take for each case.

For a script that maps different network resources for different users, you set a variable equal to oNet.UserName and, based on the value of this variable, map selected network drives to certain drive letters. The Case Else statement provides a default set of mappings for any user logon names that you haven't prepared for. Listing 1 shows a sample script. Substitute appropriate usernames and network mappings for your environment, and this script will work on any networked computer with WSH support. Notice that the script uses a variable of the string type for the username. Thus, the username values in the Select Case statement must be in quotes. If the variable were a number or Boolean value instead of a string, you wouldn't use quotes.

Finishing Touches
I've described the WshNetwork object and the Select Case statement, but the script in Listing 1 also has a few finishing touches that I use in all my scripts. To help with version control, I begin a script with a header that tells the script's name, its last revision date, and its author. MapDrive.vbs has a header to show what one looks like, but for space reasons, future scripts won't have one. The MapDrive.vbs script next has an Option Explicit statement. As I explained in "Scripting Windows Management," using this statement prevents errors introduced by mistyping variable names. If the script encounters a variable name that I didn't explicitly define, it will stop with an error instead of creating a new variable with the wrong name.

Notice also the short lines and indentation in the script. These techniques are optional, but you can see how they make the script easier to read. The statements for each case are on a line separate from the Case statements themselves. You can put the statements for each case on the same line as the conditional statement. This practice is OK if you have only a single short expression for each case. However, putting everything on one line isn't a good habit to get into. Case statements are rarely that simple; they can consist of entire miniscripts. The longer and more complex each line in a script is, the more difficult debugging becomes. In the name of your long-term scripting sanity, get into the habit of putting the statements for a case on their own line separate from the condition for which they apply.

Finally, note that the script has a lot of comments. Even though this script is pretty simple, liberally commenting your scripts is always a good idea.

To run this script, copy it into a text editor such as Notepad, edit the usernames and network paths for your network, and save the edited script as MapDrive.vbs. Run the file from the command line or by double-clicking its icon in Windows Explorer.

This month, you learned how to use WSH objects to gain access to shared network drives. Using VBScript and its Select Case statement, you created a logon script that you can tailor for your users or computers. The network mapping is also nonpersistent, which makes the script useful for shared computers. Next time, I'll show you how to programmatically install and create a default printer based on user or computer identity or on computer location. To perform this task, I'll combine another WshNetwork method—AddWindowsPrinterConnection—with VBScript's InputBox function.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish