Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


June 2002

Connecting Users to Network Resources


RSS
Subscribe to Windows IT Pro | See More VBScript Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

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.

   Previous  [1]  2  Next 


Top Viewed ArticlesView all articles
CES 2009: Ballmer Announces Windows 7, Windows Live, Live Search Milestones

During his first-ever Consumer Electronics Show (CES) 2009 keynote address last night in Las Vegas, Microsoft CEO Steve Ballmer announced the pending public availability of a feature-complete Windows 7, the final version of Windows Live Essentials, and ...

10 Reasons to Deploy Windows Vista

The decision to upgrade your XP systems to Vista is simple when you consider features such as easier backup, a great desktop search, and vastly improved security options. ...

10 Reasons Not to Deploy Windows Vista

The decision to upgrade to Vista has to make business sense, but many companies find the costs in training and application compatibility problems outweigh any benefits Vista brings. ...


Windows OSs Whitepapers Why SaaS is the Right Solution for Log Management

Related Events Virtualization Forum: Optimizing Storage, Networks, Desktops, and Security

Cloud Computing Forum: Integrating Software, Server and Storage as a Service into Your Enterprise IT Delivery Model

Virtualization Forum: Optimizing Storage, Networks, Desktops, and Security

Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2009 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing