Jump to content

scheduled script to delete stalled print jobs?


Recommended Posts

Posted

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?

Posted

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..............

 

' 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

Posted

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.

 

' 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 
' [url]http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/printing/servers/[/url]


'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...

Posted

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.

Posted

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..

 

' 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 
' [url]http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/printing/servers/[/url]


'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

Posted

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?

Posted
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:

Posted

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

Posted
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 :)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...