Contents of Guy’s Scripting Ezine No 17 – Default Printer
- This Week’s Secret
- Printer Scripts – Part 2
- Setting the default printer
- Enumerating your printers
This Week’s Secret is that it is you the reader, who drives me to master scripting. You ask questions that I had never thought of, you want scripts to do things that I had never imagined. As I love to learn, keep those questions coming!
Last week’s burning question was ‘Can you use a Computer Start Up script to assign a printer to the machine’ The answer still seems to be NO. Why I keep asking the question is because:
a) One of my clients asked me to create such a script.
b) On the face of it seems a reasonable request.
c) No-one has produced absolute proof that it cannot be done.
I have had several requests for a script which will set the default printer. The scenario is this, you press the print button in PowerPoint and instead of getting a beautiful colour picture, you get a black and white laser print out. The cause of the problem was that the wrong printer was set as the default. To prevent having to manually set the default printer every time you use a machine, let us create a script which sets the default printer and assign it at logon.
Copy the following code. Now open Notepad, paste in the text, when you have changed the UNC path, save the file with a .vbs extension. Example DefaultPrinter.vbs.
Change "\\ServerName\PrinterName" to reflect a real server and printer on your network. If you get an 800 WSH error message then you probably forgot to change the ServerName or PrinterName.
‘ Example VBScript to set the local Default Printer
‘ Guy Thomas February 2004.
Set objPrinter = CreateObject("WScript.Network")
‘ End of example VBScript
- Trap: You change ServerName to the local machine name. It should of course be the name of the network server (The name you used in the add printer script). This is what I mean by a trap, you are sitting at a machine called XPSpecial you set the path to XPSpecial\HP6L instead, where the actual device is installed and shared. For example of BigServer\HP6L.
- Line 6 can you see what sort of Object is being created? Yes we need the Network Object.
- On line 7 the Method that is applied to the printer – is SetDefaultPrinter.
- Challenge: you could add an extra line at the end
WScript.Echo "Your DEFAULT printer : " & UNCpath1
- If you are a perfectionist you could add WScript.Quit
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.
On the surface, this script just displays the network printers, it does not actually create anything new. From a learning point of view we explore a rich seam of features. The word Enumerate – meaning to count, while this not a word often used in conversation, there is a whole family of Enum methods which are useful in VBScripts. As well as EnumPrinterConnections which I use in the script below, there is also EnumNetworkDrives.
The good news is that this script will run on any Windows machine, you do not have to alter any UNC paths. Just copy and paste into notepad. Save with a .VBS extension for example EnumPrinter.vbs.
‘ Example VBScript to display your printers
‘ Guy Thomas February 2004.
Dim oPrinter, oNetwork, iCount
Set oNetwork = WScript.CreateObject("WScript.Network")
Set oPrinter = oNetwork.EnumPrinterConnections
For iCount = 0 to oPrinter.Count – 1 Step 2
WScript.Echo "Printer Port " & oPrinter.Item(iCount) & _
" = " & oPrinter.Item(iCount+1)
‘ End of example VBScript
- Names in VBScript are case insensitive, so feel free to use capitals in the middle of a word if it makes more sense.
- The key point in this script is the loop construction. What happens is this: The ‘For …. Next’, with its iCount counter, cycles through all the printers.
- Step 2 puzzled me at first, but then I realized that each ‘cycle’ gets the port name – Line 9 Item(iCount), then the share name – Item(iCount+1); so that is why you need a value of 2 for the Step command.
Challenge: Try the script without Step 2 (or substitute Step 1), and note the different output, ending with the inevitable error message.
- If you ever have to type the script form scratch, remember that Enum methods are plural methods EnumPrinterConections.
- Incidentally I have chosen the short single letter prefix in the variables. For example, oNetwork rather than objNetwork. My hidden agenda is to give you variety so that you can choose a convention that suits you.
Where you, or your users, have multiple printers it makes sense to set one of the printers as the default. Why leave it to chance when you can add a line to your logon script?
Enumerate means to count. VBScript has a family of methods using variations of Enum. While Enum does nothing in itself, it allows you to expose objects, test them, then decide which ones you wish to configure.
See more about logon printer VBScripts