Introduction to EnumPrinterConnections
EnumPrinterConnections is not a command to use in isolation. Before you try this EnumPrinterConnections
method, I suggest that you create a VBScript with AddWindowsPrinterConnection. My reasoning is that you need a printer or two before you start counting them. Topics for
EnumPrinterConnections
Enumerate Mapped Network Drives ScenarioEnumPrinterConnections reminds me of a door handle, both are useless until they are attached to an object. The EnumPrinterConnections method opens the
way for the
AddWindowsPrinterConnection or SetDefaultPrinter VBScript commands. Let us suppose that you want to script extra 'If ...then.. else' logic. Without EnumPrinterConnections, it would be difficult to get a handle on the printers,
but with EnumPrinterConnections, it's easy for the script to list the printers that are already in connected. Once you enumerate the printers, it is but a short step to manipulate them, for example,
you can use logic such as, if strUNCPrinter = HP then do nothing, else if strUNCPrinter = Epson, then set as default printer.
EnumPrinterConnections SyntaxMost VBScript commands are singular, but the Enum family have a plural ending. There is a good reason for the 's' at the end, it is because Enum commands represent an array, or collection. The syntax
however, is deceptively simple for example: objDrive = objNetwork.EnumPrinterConnections When it comes to output, which actually displays the
Enumerated printers, then we need extra VBScript commands. Therefore, it is no good just enumerating the printers, we want the script to methodically cycle through and display their names and the LPT
ports,
here is the code. For intDrive = 0 to objDrive.Count -1 Step 2 intNetLetter
= IntNetLetter +1 WScript.Echo "UNC Path " & objDrive.Item(intDrive) & " = " _ & objDrive.Item(intDrive +1) & " Mapped drive No : " & intDrive Next
Technically, EnumPrinterConnections outputs a collection of paired items. The even numbers in each pair are the printer ports and the odd numbers represent the UNC paths. Think of the collection as an array. Imagine the printer port in one column and the UNC path in the other.
You could try altering the values for Step, but I find that 2 is the best number. Step 2 makes sense, as after all these are paired items.
EnumPrinterConnections is complex to code, this is because it needs other VBScript commands. To give the script the best chance of working, we need to create one or more printers, otherwise there would be nothing for the script to
enumerate.
Pre-requisites
- The reason that we are creating extra printers here is so that we can study EnumPrinterConnections.
- On Line 10, change the server name from '\\LittleServer' to the name of your server.
- Make sure that change the printer UNC to refer to an actual printer on your network.
Instructions for AddWindowsPrinterConnection - Copy and paste the script below into notepad.
- Save the file with .vbs extension e.g. EnumPrinters.vbs.
- Double click and check in your Printers and faxes folder.
'' Printers.vbs - Windows Logon Script. ' VBScript - to map a network printer ' Author Guy Thomas http://computerperformance.co.uk/ ' Version 1.3 - April
24th 2005 ' -----------------------------------------------------------------' Option Explicit Dim objNetwork, strUNCPrinter strUNCPrinter = "\\LittleServer\HP LaserJet 4L" Set objNetwork =
CreateObject("WScript.Network") objNetwork.AddWindowsPrinterConnection strUNCPrinter objNetwork.SetDefaultPrinter "HP LaserJet 4L" WScript.Echo "Check the Printers folder for : " & strUNCPrinter
WScript.Quit
' End of Guy's Windows logon script.
Instructions to EnumPrinterConnections - Copy and paste the script below into notepad.
- Save the file with .vbs extension e.g. EnumPrinters.vbs.
- Double click the script and launch your Printers and faxes folder.
' ' EnumPrinters.vbs N.B. This script need printers ' VBScript to Enumerate Printers. ' Author Guy Thomas http://computerperformance.co.uk/ ' Version 1.8 -
April 24th 2005 ' -----------------------------------------------------------' Option Explicit Dim objNetwork, objPrinter, intDrive, intNetLetter
' This is the heart of the script ' Here is
where objPrinter enumerates the mapped drives Set objNetwork = CreateObject("WScript.Network") Set objPrinter = objNetwork.EnumPrinterConnections
' Extra section to troubleshoot If
objPrinter.Count = 0 Then WScript.Echo "Guy's warning: No Printers Mapped " Wscript.Quit(0) End If
' Here is the where the script reads the array For intDrive = 0 To
(objPrinter.Count -1) Step
2 intNetLetter = IntNetLetter +1 WScript.Echo "UNC Path " & objPrinter.Item(intDrive) & " = " _ & objPrinter.Item(intDrive +1) & " Printer : " & intDrive Next
Wscript.Quit(1)
' End of
Guy's Windows Logon Script
Learning Points
Note 1: The If... then else section around Lines 14 -18 is not strictly necessary, however I included this code to alert people who do not have any mapped network drives. Note 2: This method has two
separate functions. Firstly, the EnumPrinterConnections method itself, which makes the mapped printers 'visible'. Secondly, the objPrinter.Count (Line 21-25), which is able to read the printer names and port numbers because they
have been enumerated on line 12. Note 3: To me the most puzzling part of the script is .count -1. My latest theory is that this command tells the script to walk through the list of
printers starting with the lowest number. As ever, practical tests are the best, so, try objPrinter.count 1 or any positive number, it does not work, However, if you try -3
EnumPrinterConnections skips the first mapped pair of printer references. N.B. Extra information supplied by Mike Saunders. The reason you have to use .Count - 1 is because
Set objPrinter = objNetwork.EnumPrinterConnections initializes objPrinter as an array. Actually, I don't think it's an array, it's more like a list. If you have two printers on your computer, this is what
would happen. Let's say I have these two printers: Hplj1320.mydomain.com \\myserver\hplj1320 Hplj4100.mydomain.com \\myserver\hplj4100 objNetwork.EnumPrinterConnections would give objPrinter 4 items.
"Hplj1320.mydomain.com", "\\myserver\hplj1320", "Hplj4100.mydomain.com", "\\myserver\hplj4100". This list is indexed starting from 0. Another way it could be represented would be: objPrinter(0) =
"Hplj3120.mydomain.com" objPrinter(1) = "\\myserver\hplj1320" objPrinter(2) = "Hplj4100.mydomain.com" objPrinter(3) = "\\myserver\hplj4100" The reason for the .Count - 1 becomes
apparent when you look at the list and the code: For intDrive = 0 to objPrinter.Count -1 Step 2 The .Count method would return 4, as there are 4 items in the list. If you are starting at 0, there are
five numbers between 0 and 4: 0,1,2,3, and 4. If you tried to use objPrinter.Item(intDrive) where intDrive = 4, you'd get an out of bounds error. The reason for Step = 2 is because you are only concerned
with the UNC path of the printer, which is the second object of each tuple, in this case the even numbered items in the list. So, there you have it. MS.
Note 4: Step 2 is the best value. Step with any odd number produces a subscript out of range error, while step 4, takes giant paces and misses every other printer.
EnumPrinterConnections is a difficult technique. The best approach is to master first master the AddWindowsPrinterConnection method first then graduate to this counting and displaying method. An added bonus of starting with
AddWindowsPrinterConnection is that you will create an
actual printer to enumerate. Pay careful attention to the syntax; from the spelling with the
plural drives, to understanding the two parts, enumerating the printers, then displaying the array of LPT ports and UNC path. Once you have mastered EnumPrinterConnections, then you can play 'If...
then ... else' games
with your logon scripts. For example, if drive a printer is already connected, then remove it and then map a different printer.
See Also● Logon Script Home ●
AddWindowsPrinterConnection |