Subscribe to Windows IT Pro
July 22, 2002 12:00 AM

Connecting to Printers

Windows IT Pro
InstantDoc ID #25652
Rating: (1)
Downloads
25652.zip

Thus, the code at callout A in Listing 2 uses the Select Case statement to test for the computer name—the value of the oNetwork.ComputerName property. If the value is "Gamma", the script assigns the "\\server\printer1" path to the variable sPrintPath; if the value of the property is "Geektoy", the script assigns the "\\server\printer2"path to sPrintPath. So that I don't need to list every computer in the network, I included a Case Else option that represents the path of a typical printer connection—"\\server\printer3"—for all systems other than "Gamma" or "Geektoy". (If you skip the Case Else line and the ComputerName value passed to the script doesn't match the value in any of the Case option lines, the script simply skips to the first line after End Select.) Again, to use the script that Listing 2 shows, replace the printer paths and computer names with paths and names in your environment.

To connect to printers according to user identity, you can write a similar script that uses the name of the logged-on user (i.e., the value of oNetwork.UserName) as the Select Case argument. Web Listing 1 (http://www.winnetmag.com, InstantDoc ID 25652) shows the Select Case statement for such a script. The rest of the script would look like the Listing 2 script.

User-specific mappings aren't persistent. For example, suppose that Lisa logs on and that the Web Listing 1 statement maps her printer; Lisa then logs off. After Bruce logs on to the same computer and the statement maps his printer, he'll see only his printer—not his and Lisa's printers. When you want to disable a computer-specific mapping or a mapping that won't be replaced by another mapping, however, you need to manually remove the printer or call the WshNetwork object's RemovePrinterConnection method, supplying the printer path as an argument. You can include such a call in the logoff script for Win2K computers.

Creating a Location-Specific Printer Connection
The Listing 2 script connects one computer to a default printer, depending on the computer name. But what if you need to connect multiple computers in a location to the printer that's physically close to that location—and repeat this process for several locations? You could add to the Listing 2 script a Select Case statement with a Case option for each computer, or you could create a different version of the script for the computers in each location. In a large network, either option can become cumbersome. A third way exists, though: You can write a script that asks users to tell you where they are (e.g., library, second floor), then connects the user's system to the closest printer. (This type of script requires user input, but as long as you trust your user base to know what part of the building they're in and prompt them for the type of input you want, it should work. You can configure the script either to end or restart if users enter a location that doesn't exist.)

The script in Listing 3 creates an input box that prompts users to specify their location. The VBScript input box always displays an OK button and a Cancel button, but you must specify the user prompt and you can specify two additional optional arguments—title and default—to customize the input box's appearance. The title appears in the input box's title bar, and the default is a text string that appears in the input box's text box when the script first displays the input box to the user. The code at callout A in Listing 3 sets the prompt for the input box. The code doesn't set a title or default text, but if you want to do so, you must place the prompt, title, and default arguments in the following order:

InputBox(prompt, title, default)

The prompt and title variables are strings. Therefore, make sure to enclose the strings in quotation marks (").

The script then uses a Select Case statement to evaluate the user input and determine which printer to connect to. If the user typed an invalid choice, the script jumps to the Case Else option, which uses the MsgBox function to display a message box with text you specify and uses the WScript.Quit method to end the script. In other words, the script doesn't connect to a printer if the input provided doesn't match a valid location. Alternatively, the script could assign a generic printer or (to make the example a bit more complicated) prompt the user for a location again (in case the user mistyped the location the first time). Regardless, you must provide some way to end the script or to assign some value to sPrinterPath before the script calls the AddWindowsPrinterConnection method, or an error will occur when the script tries to connect to the printer without a path to use as an argument.

You can use Win2K Group Policy to set default printers or disable the Control Panel Add Printers applet. Also, you can hide shared printers (by ending their names in a dollar sign—$) so that users can connect only to the printers programmatically assigned to their username, computer name, or location. This setup will prevent users in Lab A from mistakenly sending their print jobs to Lab Z, three buildings away.

WshNetwork properties and methods aren't the only way to manage printers through VBScript—you could use the PrintAdmin tool in the Win2K resource kit or Windows Management Instrumentation (WMI) and the Active Directory Service Interfaces (ADSI)—but they're the simplest. Don't worry, we'll get to WMI and ADSI in good time.

Related Content:

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

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.