Guy’s Scripting Ezine 16 – Printer Scripts

Contents of Guy’s Scripting Ezine No 16 – Printer Scripts

This Week’s Secret

If something seems reasonable, but then I am told that it cannot be done, its like a red rag to a bull.  For example, I was told that you cannot assign a printer to a COMPUTER with a logon script.  Now I have a good basic VBScript that creates a printer, and it works fine when I double click the .vbs file.  If I assign my printer script to a user – no problem.  However, when I assign it to a computer as a Start Up script, it will not work.  Just to check, I created a non-printer script and assigned it to Start up and that does work.

Postscript: John G wrote in with information on RUNDLL32 PrintUI.DLL.  Here is what I found out.

Example 1: Mapping your Printer

Scenario: Your network has a printer device on a network server.  Your users would like to send their documents to this printer and your task is to assign this network printer as easily as possible.

Instructions for Mapping a Printer

Change "\\YourServer\PrinterName" to reflect a real server and a real printer on your network, otherwise you will get an 800 WSH error message.

Copy the following script.  Next open Notepad, now paste in the text, when you have altered the UNCpath, save the file with a .vbs extension, for example:  Printer.vbs.

‘ Example VBScript to map a printer on a server
‘ Guy Thomas February 2004.
‘ ******************************
Option Explicit
Dim netPrinter, UNCpath
UNCpath = "\\YourServer\PrinterName"
Set netPrinter = CreateObject("WScript.Network")
netPrinter.AddWindowsPrinterConnection UNCpath
WScript.Echo "Your printer is mapped from : " & UNCpath
WScript.Quit
‘ End of example VBScript

Note 1:  The UNC path, "\\ServerName\PrinterName",  to your printer is enclosed in speech marks.  (You have changed the names haven’t you?)

Note 2:  Line 7 Creates a network object called netPrinter.  As netPrinter a variable, you could change the name to HP6L if you wished.

Note 3:  On line 8 AddWindowsPrinterConnection is a Method.  Unlike variables, you cannot just make up names of methods.

Note 4:  Line 9 adds a WSCript.Echo which confirms the path of the mapped printer.

Guy Recommends: SolarWinds Free Wake-On-LAN UtilitySolarwinds Wake-On-LAN

Encouraging computers to sleep when they’re not in use is a great idea – until you are away from your desk and need a file on that remote sleeping machine!

WOL also has business uses for example, rousing machines so that they can have update patches applied.  My real reason for recommending you download this free tool is because it’s so much fun sending those ‘Magic Packets’. Give WOL a try – it’s free.

Download your free copy of SolarWinds Wake-On-LAN

Error Codes generated by this printer logon scripts

80070043 The network name cannot be found (Mistyped Server, or Printer name)
80070055 The local device name is already in use (Already created a printer)
800704B3 No network provider accepted the given network path
80070709 The Printer name is invalid
80071329 Object Already Exists
80072030 There is no such object on the server
8007203A The server is not operational

See a lot more about error codes here

Example 2 Delete a Printer with – RemovePrinterConnection

I recommend the RemovePrinterConnection method when testing your printer scripts.   I challenge you to create a pair of scripts, one script which creates a network printer, and another to remove it.  Always be on the lookout for such ‘mirror image’ scripts when testing and learning about VBScripting.

Instructions.

Copy and paste into notepad, save with .vbs extension.

As with Example 1, change the UNCpath.

‘ Example VBScript to remove a Printer
‘ Guy Thomas February 2004.
‘ ******************************
Option Explicit
Dim DelPrint, UNCpath
UNCpath = "\\YourServer\PrinterName"
Set DelPrint = WScript.CreateObject("WScript.Network")
DelPrint.RemovePrinterConnection UNCpath, true, true
Wscript.Echo "This printer was removed : " & UNCpath
WScript.Quit

‘ End of Example Script to remove printer

Note 1:  The first true, means force a printer disconnection, whilst the second true means update the user’s local profile.

Note 2:  Once again, see how the combination of using a variable (UNCpath) and WScript.Echo results in a neater script than just removing the printer with no confirmation.

 

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Orion performance monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Example 3: Mapping Multiple Printers

This is a powerful script which creates two printers (or more), without the need of a second script.   I will let you into a secret, this script causes more trouble than all the rest put together, so take care with the punctuation and carriage returns.

Instructions

Copy the following script.  Now open Notepad, paste in the text, when the script is ready, save the file with a .vbs extension.  Example MultiPrinter.vbs

Change the path for each variable to match a server and printer on your network.  For example, change  "\\Server1\printer1" to a print server on your network.

‘  VBScript to map two printers from two servers
‘  Guy Thomas February 2004.
‘  ******************************
Dim multiPrinter, UNCpath1, UNCpath2
UNCpath1 = "\\Server1\printer1"
UNCpath2 = "\\Server2\printer2"
Set multiPrinter = CreateObject("WScript.Network")
multiPrinter.AddWindowsPrinterConnection UNCpath1
multiPrinter.AddWindowsPrinterConnection UNCpath2
WScript.Echo "Your printer is mapped from : " & UNCpath1  _
& "and from : " & UNCpath2
WScript.Quit
‘ End of example VBScript

Note 1:  You can add easily add more printers by copying line 9 and pasting a new line 10.  Remember to add another UNCpathx statement.

Trap: If you recycle the SAME UNCpath variable, your script will only map one printer.  For example this is wrong, it will only map one printer not three.

UNCpath1 = "\\Server1\printer1"
UNCpath1 = "\\Server2\printer2"
UNCpath1 = "\\Server3\printer4"

See more on Windows 8 printer problems

Summary

Mapping a network printer is one of the classic jobs for a logon script.  When learning, it’s handy to have a mirror image – a script to disconnect a printer.  Another of my favourite learning techniques is to build up slowly, create a script to map one drive, then add code to map extra drives.

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