PowerShell Ezine, Logon Scripts

Guy's Scripting Ezine 116 - Map Multiple Printers

Guy's Scripting Ezine 116 - Map Multiple Printers

 ♣

This Week's Secret

Work smart.  This Week's Secret is pay attention to your overall strategy.  Apply techniques you acquire in one area to another area.  For example, once you learn how to map multiple network drives, then apply that knowledge to create multiple printers, or vica versa.

Avoid 'Over-think'

With VBScript, sometimes we cannot believe that a script can be so simple, consequently; we add unnecessary elements.  Worse still, we start inserting commands that cause a perfectly good script to fail.  In the case of mapping multiple printers here are two common examples of 'Over-think'.

Firstly, instead of recycling the same object for each printer, we waste time creating two network objects.  Secondly, we start looking for too many arguments.  When all we need is the name of the printer, we attempt to introduce an argument for a target machine, or we look erroneously for the equivalent of a drive letter.  The message is keep it simple and work smart, just stick with the printer's UNC path and the AddWindowsPrinterConnection verb.


If you are looking for handy network utilities, try some of the free downloads at Tools4Ever


This Week's Mission to map two printers

Our mission is to map not one, but two (or more) printers.  As VBScripts go, this is a straightforward script and consequently it is an ideal vehicle for beginners to learn the structure of this language.  Nevertheless, there are instructive pointers for all levels of ability.  I have divided our mission into three stages:

1) Map one printer
2) Map multiple printers
3) Set the default printer.

Pre-Requisites

You don't need Active Directory, what you do need is a machine with two shared printers.  Alternatively use two print servers.  Be flexible and if necessary cheat by creating 'fantasy' printers with the Add Printer Wizard.

Instructions

  1. Your server is unlikely to be called \\ grand, therefore, find and then edit the value of strUNCPrinter1. 
  2. Copy and paste the script below into notepad or get a script editor such as OnScript (free download).
  3. Save the file with .vbs extension e.g. Printer1.vbs.
  4. Double click and observe the message box and check your Printers and Faxes folder.

Stage One - Map one printer

 

' Printer1.vbs - Windows Logon Script.
' VBScript - Connect a network printer with AddWindowsPrinterConnection
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.7 - June 2006
' ------------------------------------------------------------'
Option Explicit
Dim objNetwork, strUNCPrinter1
strUNCPrinter1 = "\\grand\HPLaserJ"

' Create a network object (not Environment or AD)
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection strUNCPrinter1

' Optional command to show where to look for your printer
WScript.Echo "Check the Printers folder for : " & strUNCPrinter1

WScript.Quit

' End of Guy's printer script.

Learning Points

Note 1:  AddWindowsPrinterConnection is the key command.  Providing the client is XP or Windows 2000, you only need one argument, the UNC path to the network printer.  In this example, the printer is controlled by the variable strUNCPrinter1.   However, it is possible to use the UNC path directly, without creating a variable:
objNetwork.AddWindowsPrinterConnection \\ grand\HPLaserJ

Note 2: To help with the line numbers, and to color code your commands get OnScript (free download).

Stage Two - Map two printers

Here in Stage Two is where we fulfil our mission and add more printers.  Let us build on the success of Stage One and add an extra command, which maps a second printer.

 

' PrintersTwo.vbs - Windows Logon Script.
' VBScript - Connect two network printers
' Author Guy Thomas http://computerperformance.co.uk/
' Version 2.3 - June 2006
' ----------------------------------------------------------'
Option Explicit
Dim objNetwork, strLocal, strUNCPrinter1, strUNCPrinter2
strUNCPrinter1 = "\\grand\HPLaserJ"
strUNCPrinter2 = "\\zara\Epson"
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection strUNCPrinter1
objNetwork.AddWindowsPrinterConnection strUNCPrinter2

WScript.Quit

' End of Guy's multiple printer script

Learning Points

Note 1:  You only need one object: objNetwork.  The secret is to recycle this object by AddWindowsPrinterConnection to two different strUNCPrinter variables.

Note 2: It is straightforward to add a third printer by introducing another strUNCPrinterX and then inserting a matching objNetwork.AddWindowsPrinterConnection strUNCPrinterX statement.

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets.  IPAT cracks this problem of allocating IP addresses in networks in two ways:

For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges. 

For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers.  Download the Free IP Address Tracker

Stage Three - Bonus Set your Default Printer

Once you add a second or third printer, it makes sense to set the default printer rather than leave it to chance.  In this instance I have set the default printer to strUNCPrinter1, however, you may wish to amend this value.

 

' PrinterSetDefault.vbs - Windows Logon Script.
' VBScript - Set Default Printer
' Author Guy Thomas http://computerperformance.co.uk/
' Version 3.2 - June 2006
' -----------------------------------------------------------'
Option Explicit
Dim objNetwork, strLocal, strUNCPrinter1, strUNCPrinter2
strLocal = "HPLaserJet2420"
strUNCPrinter1 = "\\zara\HPLaserJ"
strUNCPrinter2 = "\\zara\Epson"
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection strUNCPrinter1
objNetwork.AddWindowsPrinterConnection strUNCPrinter2
objNetwork.SetDefaultPrinter strUNCPrinter1

WScript.Quit

' End of Guy's set printer script

Learning Points

Note 1:  You may have been wondering why I introduced strLocal, but not used this variable.  The answer is that I have left a little work for you:
a) Change the value of strLocal 
b) Then amend this line :
objNetwork.SetDefaultPrinter strUNCPrinter1
to
objNetwork.SetDefaultPrinter strLocal

Summary of Mapping Two Printers

Mapping two printers is not difficult.  Use this as an opportunity to apply knowledge from others areas, for example mapping multiple network drives.  Once you have created two or more printers, then it makes sense to control which printer is the default.

See more about logon printer VBScripts

Logon Printer Methods  • Ezines  • Logon Map Printer  • Logon Scripts  • Tool Kit

Ezine 2 Printers  • Ezine 5 Printers  • Ezine 16 Printers  • Ezine 17 Printers

Ezine 116 Printers Two  • Ezine 118 Printer network  • PowerShell Printers


Custom Search

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, Solarwinds have created the Free WMI Monitor so that you can actually see and understand these gems of operating system information.  Take the guess work out of which WMI counters to use for scripts.

Download your free copy of WMI Monitor

 

Home Copyright © 1999-2012 Computer Performance LTD All rights reserved

Please report a broken link, or an error.