August 14th, 2006

How Can I List All the Printers on a Remote Computer?

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I list all the printers – both local printers and network printers – on a remote computer?

— CH

SpacerHey, Scripting Guy! AnswerScript Center

Hey, CH. You know, back when he worked at the University of Washington the Scripting Guy who writes this column was asked to speak at a symposium being held as part of a conference on computers and education. To say this was a last-minute invitation would be an understatement; in fact, our hero had to start putting together his talk on the plane ride from Seattle to Florida, then finish it when he reached the convention center. When he arrived at the symposium itself, he discovered that his fellow speakers were hardly as harried as he was. Their secret: instead of creating a new talk for the symposium they decided to just deliver talks that they were already giving at the conference. In other words, they were going to give the exact same talk on two different days, but to two different audiences (or so they hoped).

Note. As a matter of fact it was a very interesting symposium, if only because the future Scripting Guy was the only person on the panel who had a talk even remotely connected to the topic. Needless to say, it proved to be the lowest-rated session at the conference. (And, let’s face it: being voted the most-boring and least-useful session at an academic conference isn’t easy.)

Now, if this future Scripting Guy was smart he would have learned that clever people can do twice the work with half as much effort. Your question, CH, is a good example of that. Because you specifically asked about listing printers on a remote computer, a smart person would write a column about that, then come back a few months later and reprint the column, only this time telling people how to list printers on the local computer. (And a smart person could get away with that because the two scripts are all-but identical.)

Of course, as people who read this column know, the Scripting Guy responsible for all this isn’t particularly smart. Instead of writing two separate columns he could only come up with one, thanks to a script that lists the printers on either a remote computer or a local computer:

strComputer = “atl-ps-01”

Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set colPrinters = objWMIService.ExecQuery(“Select * From Win32_Printer”)

For Each objPrinter in colPrinters If objPrinter.Attributes And 64 Then strPrinterType = “Local” Else strPrinterType = “Network” End If Wscript.Echo objPrinter.Name & ” — ” & strPrinterType Next

As you can see, we begin by assigning the name of the remote computer (in this case, atl-ps-01) to a variable named strComputer. What if we did want to list the printers on the local machine? No problem, and no need to read a separate column, either. Instead, just assign strComputer the name of the local machine, or assign it a dot (WMI shorthand for the local computer):

strComputer = “.”

After connecting to the WMI service on the desired computer, we then issue the following query, a query that returns a collection of all the printers installed on the computer:

Set colPrinters = objWMIService.ExecQuery(“Select * From Win32_Printer”)

If you weren’t sure just how … smart … the Scripting Guy who writes this column really is, well, what comes next should help convinced you. CH only asked for a list of printers, both local and network; however, no one mentioned whether or not the script could specify which printers were which. That could have been a whole ‘nother column; instead, we simply added a few lines of code that will make that distinction for you.

To make that distinction we first set up a For Each loop that loops through the collection of printers. Inside that loop the very first thing we do is execute this line of code:

If objPrinter.Attributes And 64 Then

What we’re doing here is checking the bitmask property Attributes to see whether bit 64 has been set. If it has, then we know that this is a local printer; in that case we set the value of the variable strPrinterType to Local:

strPrinterType = “Local”

If bit 64 has not been set then this must be a network printer; consequently we assign strPrinterType the value Network:

strPrinterType = “Network”

Two notes about this process. First, we aren’t going to talk about bitmask attributes today; for a reasonably good explanation of these babies take a look at this section of the Microsoft Windows 2000 Scripting Guide..

Second, you might be wondering why we didn’t just check the values of the Win32_Printer properties Local and Network; after all, isn’t the sole purpose of those properties to tell you whether a printer is a local printer or a network printer? Yes it is, and using these two properties is a bit easier and a bit more straightforward than using the Attributes property. However, the Local and Network properties are supported only on Windows XP and Windows Server 2003. By using the Attributes property we were able to create a generic script that will work on any version of Windows that supports WMI.

Which, come to think of it, means we missed out on yet another column: How Can I Determine Printer Types on a Windows 2000 Computer?

Sigh ….

All we have left to do now is echo back the printer name and the printer type:

Wscript.Echo objPrinter.Name & ” — ” & strPrinterType

And that should do it.

Incidentally, a professor from the UW happened to attend the symposium where the future Scripting Guy spoke. As she told him the following day, she was shocked, absolutely shocked that the Scripting Guy had poked fun at the university. However, she added, “Seeing as how you’re not a professor and really not all that smart I’m not going to report you to anyone.” You can imagine how gratified this Scripting Guy was to hear that. (Editor’s Note: People at Microsoft say pretty much the same things about this Scripting Guy. At least he’s consistent.)

Author

0 comments

Discussion are closed.