scheduled script to delete stalled print jobs?
Thought I'd put this one out to you all as it's becoming a prevelant problem on one school network.
It seems one or two print jobs like to choke the print queue for one print server and I was wondering if anyone had come up with a script that could be used to isolate a stalled printjob and delete it from the queue.
Anyone come across anything along these lines at all?
Re: scheduled script to delete stalled print jobs?
I have this script to do it, but i run it manually instead of on a schedule.
I suppose putting it on a schedule over lunch and at night would make sence, but not sure how to make it just remove the stalled jobs (this removes all jobs). oh btw PCPrintLogger is my ...print logger..............
Code:
' Define the names of the Services
sService1 = "LPDSVC"
sService2 = "PCPrintLogger"
sService3 = "Spooler"
' Create a shell object.
Set WshShell = WScript.CreateObject("WScript.Shell")
'stop services
WshShell.Run "net stop " & sService1,1,TRUE
WshShell.Run "net stop " & sService2,1,TRUE
WshShell.Run "net stop " & sService3,1,TRUE
'Delete Spool files
Set foo = CreateObject("Scripting.FileSystemObject")
foo.DeleteFile "C:\WINNT\system32\spool\PRINTERS\*.*"
'start services
WshShell.Run "net start " & sService1,1,TRUE
WshShell.Run "net start " & sService2,1,TRUE
WshShell.Run "net start " & sService3,1,TRUE
Re: scheduled script to delete stalled print jobs?
Thanks Midge... that helps a lot to fill the blanks I had... So, along with this:
http://www.activexperts.com/activmon...nting/servers/
... I think I can probably cobble together something that looks for the job that's sitting there at the top of the queue and kill that on it's own so the rest of the queue can continue on..
Re: scheduled script to delete stalled print jobs?
Hmm... still early days as yet because I have a clean queue and can't test but I thought I'd provide draft #1 of my script.
Code:
' This script looks for printjobs that have "started" but essentially hung the print queue
' Key triggers are the PrintJob.StartTime
' If this is greater than the period set then the script will delete that job
' and restart the print spooler.
'
'
' Author: Martin Smallridge, 06/02/07
' Using code from
' http://www.activexperts.com/activmon...nting/servers/
'On Error Resume Next
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oPrinters = WshNetwork.EnumPrinterConnections
Dim computerName
computerName = LCase(WshNetwork.ComputerName)
' Define the names of the Services
sService1 = "LPDSVC"
sService2 = "Spooler"
Const USE_LOCAL_TIME = True
Set DateTime = CreateObject("WbemScripting.SWbemDateTime")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colPrintJobs = objWMIService.ExecQuery _
("Select * from Win32_PrintJob")
Wscript.Echo "Print Queue, Job ID, TimeSubmitted, Total Pages, Time In Queue"
For Each objPrintJob in colPrintJobs
DateTime.Value = objPrintJob.StartTime
dtmActualTime = DateTime.GetVarDate(USE_LOCAL_TIME)
TimeinQueue = DateDiff("n", dtmActualTime, Now)
If TimeinQueue > 15 Then
strPrinterName = Split(objPrintJob.Name,",",-1,1)
Wscript.Echo strPrinterName(0) & ", " _
& objPrintJob.JobID & ", " & dtmActualTime & ", " & _
objPrintJob.TotalPages & ", " & TimeinQueue
'stop services
WshShell.Run "net stop " & sService1,1,TRUE
WshShell.Run "net stop " & sService2,1,TRUE
'Delete this job
objPrintJob.Delete_
'Delete Spool files
'Set foo = CreateObject("Scripting.FileSystemObject")
'foo.DeleteFile "C:\WINNT\system32\spool\PRINTERS\*.*"
'start services
shShell.Run "net start " & sService1,1,TRUE
WshShell.Run "net start " & sService2,1,TRUE
End If
Next
Not entirely sure if the job will be available for deletion or if indeed this approach will work yet but like I said, early days...
Re: scheduled script to delete stalled print jobs?
How often will you run this script? From the brief skim I just did of it, it will delete ALL the jobs from ALL the printers.
This might be a good thing if run at the end of each lesson, as the teacher probably won't care about the print jobs and it would just be a waste of paper, but just making sure you understand.
Re: scheduled script to delete stalled print jobs?
Hmm... There's a problem in there alright but I realised the formatting wasn't helping with tabs, etc.. so I've reformatted and edited a little..
Code:
' This script looks for printjobs that have "started" but essentially hung the print queue
' Key triggers are the PrintJob.StartTime
' If this is greater than the period set then the script will delete that job
' and restart the print spooler.
'
'
' Author: Martin Smallridge, 06/02/07
' Using code from
' http://www.activexperts.com/activmon...nting/servers/
'On Error Resume Next
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oPrinters = WshNetwork.EnumPrinterConnections
Dim computerName
computerName = LCase(WshNetwork.ComputerName)
' Define the names of the Services
sService1 = "LPDSVC"
sService2 = "Spooler"
Const USE_LOCAL_TIME = True
Set DateTime = CreateObject("WbemScripting.SWbemDateTime")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colPrintJobs = objWMIService.ExecQuery _
("Select * from Win32_PrintJob")
Wscript.Echo "Print Queue, Job ID, TimeSubmitted, Total Pages, Time In Queue"
For Each objPrintJob in colPrintJobs
DateTime.Value = objPrintJob.StartTime
dtmActualTime = DateTime.GetVarDate(USE_LOCAL_TIME)
TimeinQueue = DateDiff("n", dtmActualTime, Now)
If TimeinQueue > 15 Then
strPrinterName = Split(objPrintJob.Name,",",-1,1)
Wscript.Echo strPrinterName(0) & ", " _
& objPrintJob.JobID & ", " & dtmActualTime & ", " & _
objPrintJob.TotalPages & ", " & TimeinQueue
'stop services
WshShell.Run "net stop " & sService1,1,TRUE
WshShell.Run "net stop " & sService2,1,TRUE
'Delete this job
objPrintJob.Delete_
'start services
shShell.Run "net start " & sService1,1,TRUE
WshShell.Run "net start " & sService2,1,TRUE
End If
Next
The idea is ultimately to run every 30 minutes to remove any stalled processes because that tends to be what happens.
I've used the DateTime.Value = objPrintJob.StartTime value rather than the original "TimeSubmitted" because I'm only looking to remove the problem job so the others in the queue can carry on but not sure if that's what will actually happen at present :P
Re: scheduled script to delete stalled print jobs?
That's MUCH better now you've removed my line
foo.DeleteFile "C:\WINNT\system32\spool\PRINTERS\*.*"
Isn't a pain when you can't replicate faults so that you can test your solutions?
Re: scheduled script to delete stalled print jobs?
Quote:
Originally Posted by Midget
Isn't a pain when you can't replicate faults so that you can test your solutions?
Ain't that the truth... Where's a bug when you need one eh? ;)
I know this isn't working as it is but I'll persuade some poor teacher to do lots of printing soon and then kill it all... ooooh my name will be spoken with such respect today I'm sure...
8) :lol: :twisted:
Edit: Points to anyone who spotted the freudian slip earlier... :roll:
Re: scheduled script to delete stalled print jobs?
Quick question, the datediff function, is it in seconds or minutes?
Because if it is seconds then this will mean that hardly anyone will be allowed to print
TimeinQueue = DateDiff("n", dtmActualTime, Now)
If TimeinQueue > 15 Then
Re: scheduled script to delete stalled print jobs?
Quote:
Originally Posted by Midget
Quick question, the datediff function, is it in seconds or minutes?
Because if it is seconds then this will mean that hardly anyone will be allowed to print
TimeinQueue = DateDiff("n", dtmActualTime, Now)
If TimeinQueue > 15 Then
The "n" determines that it should be in minutes (no preceding 0) so that's covered :)