Guy's Scripting Ezine 16 - Printer Scripts
Contents of Guy's Scripting Ezine No 16 - Printer Scripts
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.
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
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.
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.
Error Codes generated by this printer logon scripts
80070043 The network name cannot be found (Mistyped Server, or Printer name)
See a lot more about error codes here
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.
Copy and paste into notepad, save with .vbs extension.
As with Example 1, change the UNCpath.
' Example VBScript to remove a 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.
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.
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.
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
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"
See more on Windows 8 printer problems
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