• subscribe
July 22, 2002 12:00 AM

Connecting to Printers

Windows IT Pro
InstantDoc ID #25652
Downloads
25652.zip

Use the WshNetwork object to direct users to the right printer

Making the right printer available to the right people in larger networks or in a company in which people move around from system to system can be complicated. Using the same printer all the time doesn't make sense for telecommuters or sales people who use laptops and regularly plug in at different locations. And when several people share a computer or one person uses more than one workstation, you need some way to provide the right printer for the given conditions. A VBScript program can automatically install and connect to printers at user logon according to user or computer identity, then unmap the printer connection when the user logs off. This scenario works best on Windows NT­based OSs such as Windows XP, Windows 2000, and NT because you can create the printer connection and install the drivers in one step. However, you can also use the technique on Windows 9x computers if you've preinstalled drivers.

As I explain in "Connecting Users to Network Resources," June 2002, InstantDoc ID 24893, Windows Script Host (WSH) abstracts the interface of many Win32 objects, including WshNetwork (aka WScript.Network), a collection of network resources, shared drives, and printers. The computer name (ComputerName), username (UserName), and computer domain (UserDomain) are all properties of the WshNetwork object. WshNetwork supports several methods for network access to drives and printers. To map a printer explicitly to a port, you use the AddPrinterConnection method. To create a Windows printer connection (and automatically install the driver on XP, Win2K, or NT), use the AddWindowsPrinterConnection method, which I'll use in the examples in this article. To set the default printer for a computer, use the SetDefaultPrinter method. You can use WshNetwork to install support for shared network printers, and you can use the Select Case statement and InputBox function to build flexibility into the process.

Creating a Generic Printer Connection
Listing 1 shows the simplest way to use VBScript to install a printer. The code in this example

  • defines the variables that the script will use (at callout A) so that if you mistype a variable name later in the code, you'll receive an error message instead of creating a new variable.
  • creates a connection to the WshNetwork object (at callout B). You must create this connection before you can call on the object's properties and methods.
  • assigns the printer's Universal Naming Convention (UNC) path to the sPrintPath variable (at callout C) so that the path is easier to refer to.
  • calls the appropriate method to create the printer connection and make it the default, supplying—as an argument to the method—the variable that represents the printer path (at callout D).

When I assign a valid path in the code at callout C in Listing 1, the script connects to the specified printer if the server is an NT-based computer. To use this script in your environment, simply replace the path in callout C with your valid UNC printer path.

You can use the same script to install support for more than one printer and make one printer the default. To do so, define a separate variable for each printer, assign a separate printer path and call the AddWindowsPrinterConnection method for each variable, and call the SetDefaultPrinter method to set one of the variables as the default printer. (Default printer settings work just as the default printer settings available from the GUI do: If you make more than one printer the default, the script will accept as the default the last printer you specified.)

Creating a Computer- or User-Specific Printer Connection
The script above is a good start, but it isn't all that powerful—you haven't yet done anything that you can't accomplish with the Net Print command or the Con2prt tool in the Microsoft Windows 2000 Resource Kit. To install printers according to computer or user identity, you need to make the script retrieve the identity and perform actions based on the retrieved data. Listing 2 shows a script that installs printers according to computer identity, so long as the computer you're running the script on has a connection to the print server.

This script first defines the variables and establishes a connection to the WshNetwork object. However, the code that the script introduces in Listing 2 exploits the Select Case statement, which makes this script much more powerful. (For a detailed discussion of the Select Case statement, see "Connecting Users to Network Resources.") Including the Select Case statement permits the script to read the local computer name and make a decision about which printer to install based on that information. The value of the computer name determines the value for sPrintPath, which is then plugged into the AddWindowsPrinterConnection and SetDefaultPrinter methods.



ARTICLE TOOLS

Comments
  • Ryan
    6 years ago
    Jul 02, 2006

    If you're looking to hide the ping window, try using the WshShell.Run method. It provides functionality for hiding the window while still alowing you to call external programs in the same manor.

    A very good resource on this is devguru.com
    (http://www.devguru.com/Technologies/wsh/quickref/wshshell_Run.html)

  • Ivan Metchev
    8 years ago
    Jun 02, 2004

    I use a laptop and conect to different networks. I have found a way of testing the network location by pinging a specific computer on the network and depending on the result the default printer is set, but it is not a very elegant solution. For each ping it opens a command prompt window. Is there a better way of testing if a specific network printer is online or a specific computer is on the network? My solution is listed below:

    Option explicit

    Dim net
    Dim strComputer
    Dim strPingResults
    Dim objShell
    Dim objScriptExec

    Set net = CreateObject("WScript.Network")
    strComputer = "Annabelle"
    Set objShell = CreateObject("WScript.Shell")
    Set objScriptExec = objShell.Exec("ping -n 2 -w 1000 " & strComputer)
    strPingResults = LCase(objScriptExec.StdOut.ReadAll)
    If InStr(strPingResults, "reply from") Then
    WScript.Echo strComputer & " responded to ping."
    net.SetDefaultPrinter "\\\\Annabelle\\hp LaserJet 1010"
    exit sub
    End If

  • stephen
    8 years ago
    Mar 02, 2004

    dear winnetmag,
    good article! connecting to laserprinters on a network is always
    a problem. Netprint cmd & Con2prt tool is usually nonexistant in
    private networks. In our network we connect to our print server
    automatically. But there is always problems with the different
    os's on the network. Scrpting is a good alternative. I don't
    script, but I'd very much like to.
    Do you know of any good sources for creating scripts, batch files and
    other programs that windows nt platforms can run to connect to laser printers.

  • Stuart Hendrie
    10 years ago
    Oct 18, 2002

    In the "by location" example is it possible to change the InputBox to a dropdown list to avoid typos on the user input side (and would make a large number of choices more managable)?

  • Michael reedeker
    10 years ago
    Sep 15, 2002

    I've used Listing_01.Script to Connect to a Printer and this works but when I use Listing_02.Script to Create a Computer-Specific Printer Connection I get an error message stating that the printer name is invalid. What puzzels me the most is when I use listing_02 with username instead of computername it does what it is suppost to do. Could you give me some advise on howe i can solve this.

You must log on before posting a comment.

Are you a new visitor? Register Here
  • SP1?
    I know there is a SP1 for SQL 2008 R2 available....and there is a "feature pack" as well... ...
  • SQL database mirroring
    I have SQL Server 2008 R2 Enterprise 64bit on Windows 2008 R2 Enterprise 64bit.  Each SQL Server has...
  • Dell Compellent Disk Drive
    Does anybody has experience with Dell Compellent Disk Drive? Basically, this system manages all disk...
  • Sql server performance tuning
    I need to find a tool that help me to optimize sql server,queries,improve the performance and solve ...