Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

On our primary windows 2003 print server we share about 500 printers. We typically have about 50 stuck print jobs. Although far from the end of the world it bugs the hell out of me and I tend to end up spending 30 odd mins a week searching for and deleting stuck jobs.

What I would really like is a script to delete all print jobs on all printers older than 24 hours.

Coming from a UNIX background I find windows scripting pretty confusing. Any advice on how I can get started with this task would be appreciated. Any complete solutions would be amazing. ;)

Cheers,

Mat.

share|improve this question

3 Answers 3

up vote 3 down vote accepted

This should be fairly simple to do in Powershell. You will find powershell a little more to your liking coming from *nix. You will however be working with WMI, which is truely a blessing and a curse.

Some example code that does what you want (NOT TESTED):

$strComputer = "."

$PrintJobs = get-wmiobject -class "Win32_PrintJob" -namespace "root\CIMV2" -computername $strComputer | Where-Object { $_.StartTime -lt $($(Get-Date).addDays(-1)) }

foreach ($job in $PrintJobs) {
    Write-Host "Canceling job $($job.JobId)"
    $job.Delete
}

Basically you will just need to get all objects from WMI where the Start Time is less than now - 24 hours.

share|improve this answer
    
Brilliant. Am I right in thinking that the printjob object will also have control functions, something like: $objItem.DeleteJob? I will just have to look at how time is handled. Thanks, I would mark you up, but not enough points yet... –  Matthew May 28 '09 at 21:00
    
Edited the script to hopefully give the exact script you need. –  Kevin Colby May 28 '09 at 21:01
    
ok, i get it. will try it in the morning. I like the way time is handled no need to count in seconds. addDays(-1) is pretty funny though ;) thanks again. Mat. –  Matthew May 28 '09 at 21:14
    
Powershell does a pretty good job of making things fairly easy. Glad to help. –  Kevin Colby May 29 '09 at 0:03

If you go into the windows 2003 resource kit, there is a tool designed to do this called Cleanspl.exe.

Resource Kit: mircosoft url /downloads/details.aspx?FamilyID=9D467A69-57FF-4AE7-96EE-B18C4790CFFD&displaylang=en

or you can script it:

echo Printers - Shutting down the print spooler
net stop "pcounter printer control"
net stop "print spooler"
echo Printers - Deleting print queues
del c:\WINDOWS\system32\spool\PRINTERS\*.* /q

net start "print spooler"
net start "pcounter printer control"
echo Printers - Print spooler Started
share|improve this answer

This could be your strategy. Stop the spooler service, delete all old files and restart the service.

Write this script code in a text file and name it "DeleteOldQueuedFile.vbs":

Dim Fso, Directory, Modified, Files
Set Fso = CreateObject("Scripting.FileSystemObject")
Set Directory = Fso.GetFolder("%systemroot%\system32\spool\printers")
Set Files = Directory.Files

For Each Modified in Files  
   If DateDiff("D", Modified.DateLastModified, Now) >= 1 Then Modified.Delete
Next

Write a batch file which you could schedule as a nightly job:

net stop spooler
DeleteOldQueuedFile.vbs
net start spooler
share|improve this answer
    
Thanks, It is interesting to see the difference in strategies. It is kind of strange, I am more familiar with working with the file system, as your script does, but the first script has given me a insight into the potential of WMI which frankly I was unaware of. Thanks again. It is good to see that not only is there more than one way to achieve my goal but also that I can treat the windows print spool as just a bunch of files. I take it that the print spooler locks all files in the spool directory - hence why you stop the service. That is potentialy a lot of file handles... –  Matthew May 28 '09 at 21:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.