Windows Logon Scripts - EnumPrinterConnections
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 Scenario
EnumPrinterConnections 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.
Most 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
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.
Instructions for AddWindowsPrinterConnection
' Printers.vbs - Windows Logon Script.
I like the Permissions Monitor because it enables me to see quickly WHO has permissions to do WHAT. When you launch this tool it analyzes a users effective NTFS permissions for a specific file or folder, takes into account network share access, then displays the results in a nifty desktop dashboard!
Think of all the frustration that this free utility saves when you are troubleshooting authorization problems for users access to a resource. Give this permissions monitor a try - it's free!
Instructions to EnumPrinterConnections
' EnumPrinters.vbs N.B. This script need printers
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
Hplj1320.mydomain.com \\myserver\hplj1320 Hplj4100.mydomain.com \\myserver\hplj4100
objNetwork.EnumPrinterConnections would give objPrinter 4 items.
The reason for the .Count - 1 becomes
apparent when you look at the list and the code:
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.
Guy Recommends SolarWinds' Free Network Monitor
Thus utility makes it easy to check the health of a router or firewall. Check the real-time performance, and availability statistics, for any device on your network. Get started with an extensive collection of "out-of-the-box" monitors for popular network devices. Give Network Monitor a whirl - it's free. Download your free Network Device Monitor
If you need more comprehensive
network analysis software:
Use PowerShell for Logon Scripts
VBScripts are being superseded by PowerShell .ps1 files. While PowerShell is used mainly for configuring the operating system, it's possible to use PowerShell's cmdlets to work with methods such as EnumPrinterConnections. The technique is to create a ComObject, which can act as a wrapper for familiar VBScript commands. Here is example of PowerShell's New-Object cmdlet working with EnumPrinterConnections:
# PowerShell Logon 3 Script Example
You could save these instructions in a .ps1 file. However, the hard part is executing this .ps1 file as a logon script. See more about assigning PowerShell logon scripts.
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 more printer logon scripts examples