VBS Logon Script - AddWindowsPrinterConnection
Introduction to Logon Printer Script
Users love to print. One of the classic jobs of a Windows logon scripts is to provide applications such as Word with a suitable network printer. I will show you how to write a logon script in VBS using the AddWindowsPrinterConnection method. The best client to test the script is XP, however the VBScript code also works with Windows 2003 and even NT.4. However, Windows 9x are more difficult because they need a of copy the driver on the client.
Topics for Printer Scripts
Our mission is to create a script, which provides the user with a shared printer. As so often, the script mimics your manual actions, in this instance adding a network printer in the Printers (and Faxes) folder. The concept is similar to mapping a network drive, but creating a printer rather than a home directory. Instead of a drive letter in explorer, the users get an icon in the Printers folder.
Map Printer Methods
Printer VBScripts follow the classic format of object, method, and value. We create the network object called objNetwork. Next we employ the AddWindowsPrinterConnection method to create the network printer. Finally we get a new printer icon in the Printers and Faxes folder. What gives any scripting language its power is the methods or verbs, in this case AddWindowsPrinterConnection, in other scripts SetDefaultPrinter or even RemovePrinterConnection.
Our objective is to create a printer icon, which
connects to the network printer called \\zara\HP LaserJet. Printer scripts employ a variety of different methods; in this instance, we concentrate on AddWindowsPrinterConnection. If you have Windows 2003 or XP
clients, the procedure is straightforward; this method only needs one argument, namely the UNC path to the shared printer. For example:
Note that in the case of Windows 2003 and XP clients, there is no argument to specify the driver; the client will automatically download the correct driver from the print server.
Pre-requisites to Map Printers
You need a machine (server) with a shared printer!
Instructions for a Simple Printer Logon Script
' Printers.vbs - Windows Logon Script.
Note 1: I do realize that scripts go wrong. However, with printer scripts, it's often the result of 'over think', so begin with simple plan and pay attention to the syntax. For instance, this basic script needs no commas. automatically.
Note 2: The AddWindowsPrinterConnection only requires one 'argument' the UNC path of the printer.
Note 3: This script conforms to the classic VBScript structure, object, method, and value. We create a network object, objNetwork, apply the AddWindowsPrinterConnection method and then assign the value of your shared network printer.
Note 4: I deliberately made Example 1 as short as possible so that it would highlight the essential commands. Normally, as you will see in example 2, I include a header section and declare variables.
Guy Recommends SolarWinds' Free Network Monitor
Thus utility makes it easy to check the health of a router or firewall. Check the real-time performance, and availability statistics, for any device on your network. Get started with an extensive collection of "out-of-the-box" monitors for popular network devices. Give Network Monitor a whirl - it's free. Download your free Network Device Monitor
If you need more comprehensive
network analysis software:
This script has the same core as the first example but with a few extra coding niceties. As you get to know my VBScript style, so you will see my familiar heading section, liberal use of variables, and a WScript.Echo message box to confirm what has happened.
' PrintersLong.vbs - Windows Logon Script.
VBS Learning Points
Note 1: Option Explicit forces us to declare variables before we use them in the VBScript. The idea is to reduce spelling mistakes.
Note 2: One reason that I like to employ variables is to make it easier to troubleshoot in general and display messages with WScript.Echo in particular.
Note 3: To help with the line numbers, and to color code your commands get OnScript (free download).
I like the Permissions Monitor because it enables me to see quickly WHO has permissions to do WHAT. When you launch this tool it analyzes a users effective NTFS permissions for a specific file or folder, takes into account network share access, then displays the results in a nifty desktop dashboard!
Think of all the frustration that this free utility saves when you are troubleshooting authorization problems for users access to a resource. Give this permissions monitor a try - it's free!
Pre-requisites for AddWindowsPrinterConnection
Instructions for a Windows 9x Printer Logon Script
' PrintersWin98.vbs - Windows Logon Script.
Note 1: Only use Example 3 if you have Windows 9x clients.
Note 2: Unlike the previous examples, you need a second argument for the driver, and a comma.
Note 3: There is also an option parameter to specify the LPT port, but the default LPT1 suits most installations so remember it is only an option. For example instead of strUNCPrinter, strPrnDrv, you need three parts strUNCPrinter, strPrnDrv, strPort.
Mapping Printers is a classic job for a Windows logon script. All you need to test this script is a shared network printer, the key method is AddWindowsPrinterConnection. The script works on XP, Windows 2003 and NT 4.0 clients. However, if you have Windows 9x machines, they as usual they need extra arguments.
This page was designed to get you started mapping network printers. For your next step, see below, or in the menus to left how you can set the default printer and perform other printer scripting tasks.
See more printer logon scripts examples