May 2003

More About How to Assign Network Resources

Your questions prompt a closer look
RSS
Subscribe to Windows IT Pro | See More Task Automation Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

In "Connecting to Printers," August 2002, http://www.winnetmag.com, InstantDoc ID 25652, and "Connecting Users to Network Resources," June 2002, http://www.winnetmag.com, InstantDoc ID 24893, I explained how to assign network printers and shares to users based on computer names and usernames. In response to these articles, readers sent email messages that asked whether they could make such assignments for more than one user or computer at a time. You can, but it requires a little more work than making selections based on individual names. Let's look at how you can assign network resources to computers with similar names and to users based on their group memberships.

Assigning Resources to Similarly Named Computers
After reading "Connecting to Printers," a reader contacted me with this question: "Suppose that I have 20 Windows 2000 PCs (named Gamma1, Gamma2 ... Gamma20) that I need to set to printer1. Can I use a Select Case statement and test for Case "Gamma*" to access those PCs?" If you used the Select Case statement as you've described, VBScript would try to match the string Gamma* with the exact value Gamma*—in other words, the only matches would be for computers named Gamma*. The wildcard character that you're accustomed to using in searches or from the command line won't work without VBScript's RegExp object, which I haven't discussed yet in a Scripting Solutions column. However, if you've named computers based on a naming scheme organized according to computer location, function, or some other rule, you can look for matches based on the common root of those names.

For example, suppose that your company has workstations set up in the lab, library, and reception area, and you've named these workstations according to their location. The lab workstations have names that start with the prefix LAB, the library workstations have names that start with LIBR, and the reception area workstations have names that start with REC. All the workstation names end with two-digit numbers, so the workstation names look something like LAB01, LIBR19, and RECP03.

Listing 1 shows SetPrinter.vbs, a script that sets the default printers for these three groups of workstations. SetPrinter.vbs starts by defining the variables and creating the oNetwork object to represent a WshNetwork object. Next, the script uses the Left function to capture a three-character prefix from each workstation name, then assigns that prefix to a variable.

VBScript's Left and Right functions count the number of characters in a string and return the number of characters you specify. The Left function returns the specified number of characters from the left side of the string, whereas the Right function returns the specified number of characters from the right side of the string. The syntax of both functions is simple: They take as arguments the string on which to operate and the number of characters to return. So, for example, if you type

Left("gorilla", 2)

the Left function returns the value go. If you type

Right("gorilla", 5)

the Right function returns the value rilla.

The first argument of the Left or Right function doesn't have to be a literal string; you can instead use code that returns a string at runtime. As the code at callout A in Listing 1 shows, SetPrinter.vbs uses the WshNetwork object's computerName property to obtain the computer's name at runtime. The Left function then captures the first three letters of the computer's name and assigns them to the string variable called sShort. As this script illustrates, the number of characters in sShort doesn't need to be the same as the number of characters in the prefix that you're using in the computers' names. As long as the characters in sShort uniquely identify a particular group of computers, the script will work.

Finally, SetPrinter.vbs uses the Select Case statement to compare the characters in sShort with the strings Lib, Lab, and Rec. When sShort matches one of those strings, the script assigns the appropriate printer path to the sPrintPath variable. SetPrinter.vbs then uses the sPrintPath variable with the WshNetwork object's AddWindowsPrinterConnection and SetDefaultPrinter methods to add a printer connection and assign the default printer, respectively.

   Previous  [1]  2  3  Next 


Windows IT Pro Community
Blogs





Top Viewed ArticlesView all articles
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. ...

WinInfo Short Takes: CES 2009 Special Edition

An often irreverent look at some of the week's other CES 2009 news, including covering the Vegas spectacle from the comfort of my own home, Windows 7 public beta, a weird Microsoft song application, Palm Pre, pending Microsoft mobile moves, and much more ...


Task Automation Whitepapers Essential Guide to E-discovery and Recovery for Microsoft Exchange

Continuous Data Protection and Recovery for Microsoft Exchange

Protecting (You and) Your Data with Exchange Server 2007

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!

Task Automation eBooks Spam Fighting and Email Security for the 21st Century

A Guide to Windows Certification and Public Keys

Keeping Your Business Safe from Attack: Patch Management

Related Task Automation 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.


SQL Server Magazine Office & SharePoint Pro Windows Dev Pro ITTV
IT Library Technology Resource Directory Connected Home 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