Hey, Scripting Guy! How can I print a test page to a printer?
Hey, RR. As long as you’re running Windows XP this is an easy one; that’s because the revised Win32_Printer class introduced in Windows XP includes a method called PrintTestPage. As you might expect, this means you can bind to a given printer on a computer and then call the PrintTestPage method to, well, print a test page.
Here’s a sample script that does just that:
strComputer = “.”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)
Set colPrinters = objWMIService.ExecQuery _
(“Select * from Win32_Printer Where DeviceID = ‘\\\\atl-ps-01\\color-printer'”)
For Each objPrinter in colPrinters
errReturn = objPrinter.PrintTestPage
If errReturn = 0 Then
Wscript.Echo “The test page was printed successfully.”
Wscript.Echo “The test page could not be printed.”
All we do in this script is connect to the WMI service, then run a query that binds us to the printer with the DeviceID \\atl-ps-01\color-printer. Note that because we are using the DeviceID as part of a Where clause (Where DeviceID =) we need to “escape” each \ in the printer path. Consequently we need to use this as the DeviceID:
Just another one of the little eccentricities we’ve come to love about WMI: any time you use a \ in a Where clause you must preface it with a second slash. If you have back-to-back \\’s like we do here, you must preface each slash with a second slash, yielding this crazy-looking construct: \\\\. But, rules are rules, right?
After binding to the printer (and our collection will consist of only one printer, because DeviceIDs must be unique) we call the PrintTestPage method, capturing the return value in a variable named errReturn. If errReturn is 0 that means the test page printed successfully; if errReturn is anything but 0 then something went wrong and the test page could not be printed. Either way we echo the appropriate message to the screen.
Like we said, pretty easy. What makes this especially nice is : 1) because this is WMI, we can print test pages from remote machines just as easily as we can from the local machine; and, 2) the return value will tell us whether or not the print test succeeded. No need to walk all the way to the printer only to find out that, for some reason, the print test failed.
Of course, that’s all well and good if you’re running Windows XP. But what if you aren’t, what if you’re still running Windows 2000? Well, in that case the news isn’t quite so good; we don’t know of any “pure” scripting way to print a test page, at least not any way built into the operating system. However, you can print a test page from the command prompt using this command:
rundll32 printui.dll,PrintUIEntry /k /n “\\atl-ps-01\color-printer”
If you wanted to, you could wrap this command up in a VBScript and execute the test page from there. For more information about running command line tools from within a script take a gander at this classic Tales from the Script column (it’s actually the first Tales ever written).