+ Post New Thread
Results 1 to 10 of 10
Coding Thread, scheduled script to delete stalled print jobs? in Coding and Web Development; Thought I'd put this one out to you all as it's becoming a prevelant problem on one school network. It ...
  1. #1
    contink's Avatar
    Join Date
    Jul 2006
    Location
    South Yorkshire
    Posts
    3,791
    Thank Post
    303
    Thanked 327 Times in 233 Posts
    Rep Power
    119

    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?

  2. #2
    Midget's Avatar
    Join Date
    Oct 2006
    Location
    In a Server Room cutting through a forest of Cat5e
    Posts
    1,298
    Thank Post
    5
    Thanked 59 Times in 49 Posts
    Rep Power
    40

    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

  3. #3
    contink's Avatar
    Join Date
    Jul 2006
    Location
    South Yorkshire
    Posts
    3,791
    Thank Post
    303
    Thanked 327 Times in 233 Posts
    Rep Power
    119

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

  4. #4
    contink's Avatar
    Join Date
    Jul 2006
    Location
    South Yorkshire
    Posts
    3,791
    Thank Post
    303
    Thanked 327 Times in 233 Posts
    Rep Power
    119

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

  5. #5
    Midget's Avatar
    Join Date
    Oct 2006
    Location
    In a Server Room cutting through a forest of Cat5e
    Posts
    1,298
    Thank Post
    5
    Thanked 59 Times in 49 Posts
    Rep Power
    40

    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.

  6. #6
    contink's Avatar
    Join Date
    Jul 2006
    Location
    South Yorkshire
    Posts
    3,791
    Thank Post
    303
    Thanked 327 Times in 233 Posts
    Rep Power
    119

    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

  7. #7
    Midget's Avatar
    Join Date
    Oct 2006
    Location
    In a Server Room cutting through a forest of Cat5e
    Posts
    1,298
    Thank Post
    5
    Thanked 59 Times in 49 Posts
    Rep Power
    40

    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?

  8. #8
    contink's Avatar
    Join Date
    Jul 2006
    Location
    South Yorkshire
    Posts
    3,791
    Thank Post
    303
    Thanked 327 Times in 233 Posts
    Rep Power
    119

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


    Edit: Points to anyone who spotted the freudian slip earlier...

  9. #9
    Midget's Avatar
    Join Date
    Oct 2006
    Location
    In a Server Room cutting through a forest of Cat5e
    Posts
    1,298
    Thank Post
    5
    Thanked 59 Times in 49 Posts
    Rep Power
    40

    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

  10. #10
    contink's Avatar
    Join Date
    Jul 2006
    Location
    South Yorkshire
    Posts
    3,791
    Thank Post
    303
    Thanked 327 Times in 233 Posts
    Rep Power
    119

    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



SHARE:
+ Post New Thread

Similar Threads

  1. Adding User Name To Print Jobs
    By Richie1972 in forum Network and Classroom Management
    Replies: 20
    Last Post: 26th August 2014, 05:54 PM
  2. Script to Delete Profiles - PLEASE HELP
    By Mr_M_Cox in forum Scripts
    Replies: 13
    Last Post: 22nd February 2013, 04:53 PM
  3. need a script to delete shortcuts
    By philtomo-25 in forum Scripts
    Replies: 2
    Last Post: 1st November 2007, 04:50 PM
  4. Redirecting print jobs
    By SteveBentley in forum How do you do....it?
    Replies: 1
    Last Post: 5th June 2007, 12:56 PM
  5. print manager logging jobs
    By alonebfg in forum Windows
    Replies: 7
    Last Post: 4th November 2006, 10:12 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •