Jump to content

Recommended Posts

Posted (edited)

At present I'm waiting to see if this script works when the conditions present themselves (ie: a stalled print queue) so consider it draft #1.

 

 

 

 

' This script looks for printjobs that have "started" but essentially hung the print queue
' Key triggers are 
'  - PrintJob.TimeSubmitted
'  - PrintJob.StatusMask
'  - JobPosition (customDim)
'
' If the job is more than 15 minutes old, has a "printing" status 
' and is the first job in the queue 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, Time Submitted, Total Pages"

Dim JobPosition
JobPosition = 1

For Each objPrintJob in colPrintJobs
 DateTime.Value = objPrinter.TimeSubmitted
 dtmActualTime = DateTime.GetVarDate(USE_LOCAL_TIME)
 TimeSinceSubmit = DateDiff("n", dtmActualTime, Now)
 If TimeSinceSubmit > 15 And objPrintJob.StatusMask = 16 And JobPosition < 2 Then
 	
   strPrinterName = Split(objPrintJob.Name,",",-1,1)
   Wscript.Echo strPrinterName(0) & ", " _
		& objPrintJob.JobID & ", " & dtmActualTime & ", " _
     & objPrintJob.TotalPages & ", " & TimeSinceSubmit

   'stop services
   WshShell.Run "net stop " & sService1,1,TRUE
   WshShell.Run "net stop " & sService2,1,TRUE

   'Delete this job
   objPrintJob.Delete_

   'start services
   WshShell.Run "net start " & sService1,1,TRUE
   WshShell.Run "net start " & sService2,1,TRUE

 End If
 JobPosition = JobPosition + 1		
 ' We only want to kill Job #1 if it's stalled
 ' so if we make it past job #1 it's working
Next

 

 

Like I said, no idea if this works as yet but I'll update once I get a chance to try it against the conditions we hit here every few days... ie: a stalled print queue.

Edited by contink
quick fix
Posted

After waiting a while for someone to kill the print queue I finally got a chance to test this out properly and ended up re-writing the thing to handle things properly.

 

The script now tries a couple of things.

 

1. It tests the print queue to see if there's any print jobs either stalled or taking too long to complete (set as the "StallTime" variable).

 

2. If yes it then forces the print spooler to get restarted

 

3. If the job is still "stalled" or running after StallTime + 15 minutes it kills the spooler, deletes the stalled job and then restarts the spooler.

 

 

Of course this could result in someone ending up with a print job that would take considerable time to complete having it kicked off the system after so many minutes. Perhaps not ideal but having seen a school printer hogged for over an hour in the past I actually think it has a salutory effect on printer usage.

 

One workaround is to set the scheduling system so that you leave a window during lunchtimes, before class and after school when the script doesn't run as an auto schedule routine.

 

 

Anyway, comments welcome and especially any suggestions for reducing the load.. I know it's a clutz like approach so any slimming techniques would be useful :)

check_clear_stalled_queue.vbs

Posted

I get an error on Set DateTime = CreateObject("WbemScripting.SWbemDateTime")

 

ActiveX component can't create object

 

 

when using Windows 2000Server

Posted
I get an error on Set DateTime = CreateObject("WbemScripting.SWbemDateTime")

 

ActiveX component can't create object

 

 

when using Windows 2000Server

Hmm.. may be win2k related then as I'm running it on a Win2k3 server..

 

Problem is I know very little about VBscript so I'll hope someone else can shed some light on the problem.

Posted
ahhh thats ok then, as you were.

 

/me goes off to ask for a new server :wink:

LOL... good luck selling that one as the reason... :)

 

If I figure out what the differences are and how to do a comparison between the two dates I'll write that in but right now I know my limits.. (see that dot on the horizon behind me, that's the line.. :D)

Posted

I get the exact same error on my win2k print server.

 

It's going to be replaced this year with a 2003 one or possible pykota if all my printers work with it.

 

Ben

  • 1 month later...
Posted

Seems I need to do a little more work on this script as a bug appeared with it.

 

It'd appear that the script killed a print queue at some point but then failed to restart it again.

 

I need to think about how best to avoid that little problem but basically it's a current "feature" that you need to be aware of.

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