Managing Network Printers
Microsoft® Windows® 2000 Scripting Guide
Managing printer connections on user computers is an important part of system administration. When a new printer comes online, you do not have to send instructions on how to connect to this device; instead, you can simply include code in a logon script that automatically makes this connection for a user. Likewise, when a printer is removed from the network, you can remove the printer connection, preventing the problems likely to arise when users try to print to a printer that no longer exists.
The WshNetwork object provides methods that enable your scripts to add or remove printer connections, to set the default printer, and to list the current printer connections on a computer.
Adding Printer Connections
Two WshNetwork methods enable your scripts to add printer connections: AddWindowsPrinterConnection and AddPrinterConnection.
The AddWindowsPrinterConnection method enables a script to add a Windows-based printer connection (like using the Control Panel Add Printer Wizard), while the AddPrinterConnection method enables a script to add an MS-DOS®-based printer connection.
Adding Windows-based Printer Connections
When used to add a Windows-based printer connection to a Windows NTbased operating system such as Windows 2000, Windows XP, or Windows NT, the AddWindowsPrinterConnection method accepts, as its only required parameter, the UNC path of the printer.
The script in Listing 3.38 adds a connection to the network printer with the UNC path \\HRServer01\Printer1.
Listing 3.38 Adding a Windows-based printer connection (Windows NTbased operating system)
Set objNetwork = Wscript.CreateObject("WScript.Network") objNetwork.AddWindowsPrinterConnection "\\HRServer01\Printer1"
When used to add a Windows-based printer connection to Windows 95, Windows 98, or Windows Me, the AddWindowsPrinterConnection method requires two parameters: the UNC path of the printer and the name of the printer driver (which must already be installed on the computer). In addition, when used with these operating systems, the method accepts a third, optional, parameter that specifies the local port through which the printer should be made available.
Adding MS-DOS -based printer connections
The AddPrinterConnection method enables a script to add an MS-DOS-based printer connection. The method takes two required parameters: the local port through which the printer will be available and the UNC path of the network printer.
The AddPrinterConnection method also lets you add the mapping to the user profile. If you want to do this, set the updateProfile Boolean argument to True. The user name and the password parameters let you connect to the specified printer by using another users credentials.
Unlike the AddWindowsPrinterConnection method, the AddPrinterConnection method requires you to specify a port name.
Removing a Printer Connection
To remove a printer connection, use the RemovePrinterConnection method.
WshNetwork.RemovePrinterConnection(printerName, [forced], [updateProfile])
The first argument identifies the shared printer. The other two optional arguments let you specify:
Whether the disconnection should be forced, even if the printer is in use.
Whether the user profile should be updated to reflect the disconnection.
The RemovePrinterConnection method removes both Windows and MS-DOS-based printer connections. If the printer was connected using AddPrinterConnection, the printerName argument must be the same as the printers local port. If the printer was set up using the AddWindowsPrinterConnection method or was added manually through the Add Printer wizard, the printerName argument must be the printers UNC name.
For example, this command removes the connection to the printer \\atl-ps-01\colorprinter.
Set objNetwork = WScript.CreateObject("WScript.Network") objNetwork.RemovePrinterConnection "\\atl-ps-01\colorprinter"
Enumerating the Available Printers
To obtain a list of the printers set up on a computer, you use code similar to that used to list the mapped network drives.
You use the EnumPrinterConnections method to obtain a WshCollection object where each network printer is made up of two elements in the collection. The even-positioned item contains the printers local name or port. The odd-positioned item contains the UNC name. As shown in Listing 3.39, you can use a For Next loop with the step value 2 to collect all the information you need.
Listing 3.39 Enumerating Available Printers
1 2 3 4 5
Set objNetwork = WScript.CreateObject("WScript.Network") Set colPrinters = objNetwork.EnumPrinterConnections For i = 0 to colPrinters.Count -1 Step 2 Wscript.Echo colPrinters.Item(i) & vbTab & colPrinters.Item (i + 1) Next
When run under CScript, output similar to this is displayed in the command window:
LPT1: Art Department Printer XRX00034716DD75 \\atl-prn-xrx\plotter XRX0000AA622E89 \\atl-prn-xrx\colorprinter
Setting the Default Printer
Many users print documents by clicking the printer icon within their application; in most cases that means that documents are automatically sent to the users default printer. By assigning different users different default printers, you can help divide the print load among your print devices and ensure that documents are printed quickly and efficiently.
The SetDefaultPrinter method lets you assign a specific printer the role of the default printer. SetDefaultPrinter uses the following syntax:
The printerName parameter is the UNC name of the printer. (If the printer is a local printer, you can also use a port name such as LPT1.) This script sets the printer \\atl-ps-01\colorprinter as the default:
Set objNetwork = WScript.CreateObject("WScript.Network") objNetwork.SetDefaultPrinter("\\atl-ps-01\colorprinter")
The SetDefaultPrinter method cannot retrieve the current default printer.