Posted On March 31, 2019

Script to Purge Stuck Print Jobs

kimconnect 0 comments
blog.KimConnect.com >> Windows >> Script to Purge Stuck Print Jobs
Create a Task Scheduler:
- Program to run: powershell.exe
- Argument: -ExecutionPolicy Bypass c:\scripts\clearStuckQueue.ps1
- Trigger: daily, repeat every 5 minutes

# Clear Print Queue from Stuck Jobs
#---------------------------------------------
# define "stuck" print jobs
$stuck = Get-ChildItem -path C:\Windows\System32\spool\PRINTERS | where {$_.Lastwritetime -lt (date).addminutes(-60)}
If ($stuck) {
net stop spooler /y
sleep 5;
$stuck | remove-item -Force;
Start-Service -Name spooler
}
#-----------------------------------------

#Alternative
#-------------------------------------------
Get-Printer -ComputerName localhost | get-printjob | where{$_.SubmittedTime -lt (date).addminutes(-1) } | Remove-PrintJob
#-------------------------------------------

Experimental
#-----------------------------------------
$computer = "localhost";
#Query for errors in print queue
Write-Verbose "Checking for errors in print queue on $computer."
$queue = Get-WMIObject win32_printer -computername $computer | where {$_.PrinterState -match '[28]'}
#Execute when error condition exists
If ($queue)
{
#Clear out errors
Write-Verbose "Clearing out print jobs with errors..."
$queue | % {
$_.CancelAllJobs() | Out-Null
}
}
#-----------------------------------------

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

How to configure IPMonitor for a server

After enabling the SNMP service via Server Manager > Features, go into Services and locate…

A Few Todos while Securing Apache Server

- Install mod_security - Set expose_php = Off in php.ini - Set SecResponseBodyAccess Off in…

Websense Filtering Precedence

1.  Username 2.  Workstation IP 3.  Network Range 4.  User Groups 5.  OU Structure 6. …