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

Active Directory Domain Accounts Security Hardening

1. Disable or rename Administrator account1a. Create an alternate service account1b. Discover where it's being…

Add a Domain Group to Local Administrators Group

$checkGroup="Administrators" $addMember="KIMCONNECT\Desktop Admins" # Dynamic Credential $who = whoami if ($who.substring($who.length-2, 2) -eq "-admin"){$username=$who;} else…

How to Install & Configure Pihole on Ubuntu 20.04

1. Installation- Run these commands: # sudo apt-get install gamin -ysudo curl -sSL https://raw.githubusercontent.com/pi-hole/pi-hole/master/automated%20install/basic-install.sh |…