How Can I Purge a Print Queue on a Print Server?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I purge a print queue on a print server? I need to do this on both Windows 2000 and Windows Server 2003.

— KD

SpacerHey, Scripting Guy! AnswerScript Center

Hey, KD. And hey: no fair! Lots of times we take the easy way out in this column: we provide an answer that works only on Windows XP or Windows Server 2003, and then say, “If there’s enough interest we’ll see if we can find a way to do this on Windows 2000 as well.” If there’s not enough interest, well, that just means we didn’t have to work very hard on that particular column.

But you’re cheating: you specifically mentioned both Windows 2000 and Windows Server 2003. That makes it a bit harder for us to get away with the old “If there’s enough interest” ploy. Pretty clever, KD. Pretty clever.

But one thing about the Scripting Guys: we always respond to a challenge. (We don’t always respond well to a challenge, but we do respond.) You say you need to purge a print queue on Windows 2000. Okey-doke; here’s an ADSI script that does just that:

Set objPrinter = GetObject(“WinNT://atl-ps-01/ArtDepartmentPrinter”)
objPrinter.Purge

No, we’re not taking shortcuts here: that is the entire script. In the first line, we bind to the printer ArtDepartmentPrinter on the print server atl-ps-01. And in the second line we call the Purge method to delete all the print jobs in the queue. Just that easy, just that quick.

In other words, one down (Windows 2000) and one to go (Windows Server 2003). Now, technically, we don’t have to show you another script; after all, the preceding script also works on Windows Server 2003. But just to prove that we aren’t completely lazy, here’s a second script, one that uses WMI but runs only on Windows Server 2003 and Windows XP. Let’s look at and explain the code, then talk about why you might want to use this script to manage print queues:

strComputer = “atl-ps-01”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set colInstalledPrinters = objWMIService.ExecQuery _ (“Select * from Win32_Printer Where Name = ‘ArtDepartmentPrinter'”)

For Each objPrinter in colInstalledPrinters objPrinter.CancelAllJobs() Next

In this script we start off by connecting to the WMI service on the remote computer atl-ps-01. We then use this line of code to return a collection consisting of all the printers named ArtDepartmentPrinter (a collection that will have, at most, a single item, seeing as how printer names must be unique on a print server):

Set colInstalledPrinters =  objWMIService.ExecQuery _
    (“Select * from Win32_Printer Where Name = ‘ArtDepartmentPrinter'”)

From there we create a For Each loop to loop through the collection, calling the CancelAllJobs() method to purge all the print jobs for each print queue in the collection.

That’s pretty straightforward, but the first script we showed you is not only shorter but it runs on Windows 2000 as well. So why would ever you want to use the WMI version? Well, if all you’ll ever need to do is purge one specific print queue then we can’t really think of a good reason; go ahead and use the first script. The value of the second script can be found in WMI’s ability to manage printers and print queues, capabilities (found only in Windows XP and Windows Server 2003) that far exceed ADSI’s ability to manage printers and print queues.

For example, suppose you want to purge only print queues containing x number of print jobs or only print queues on your color printers. Those tasks are far easier to do using WMI than using ADSI. And what if you want to install (or uninstall) printers, printer drivers, or printer ports? You can do that with WMI; you can’t do that with ADSI. Thus WMI scripting offers a lot of long-term advantages, even if, in the short-term, and for a very specific task, ADSI might seem better.

And that’s why we recommend WMI for doing most system administration tasks, leaving ADSI for chores involving users and groups and stuff like that. You can take that basic WMI script and modify it to carry out all sorts of tasks; that isn’t necessarily true with the ADSI version.

0 comments

Discussion is closed.

Feedback usabilla icon