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 


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 


Reader Comments
No scripting on setting default printer appears.

MrTwistoff January 29, 2005 (Article Rating: )


You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
WinInfo Short Takes: Week of November 24, 2008

An often irreverent look at some of the week's other news, including a Vista Capable dismissal request, Zune price reductions, Morrow musings, Novell and Microsoft sitting in a tree ... two years later, Yahoo!, IE 6 on Windows Mobile, and so much more ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

PsExec

This freeware utility lets you execute processes on a remote system and redirect output to the local system. ...


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 Concrete Ways to Make Sure Your SharePoint Deployment Doesn't Blow Up

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.


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 © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing