How to modify this print queue clear powershell script to exclude two printers?

I found a powershell script here that automatically cancels print jobs after a certain amount of time by using the System.Management.ManagementDateTimeConverter class to convert WMI dates to .Net dates.

This is the script:

$PrintJobs = Get-WmiObject Win32_PrintJob | Where-Object { [System.Management.ManagementDateTimeConverter]::ToDateTime($_.TimeSubmitted) -lt $($(Get-Date).addMinutes(-2))}

foreach ($job in $PrintJobs) 
{    
   "Canceling job $($job.JobId)"    
}

Open in new window


I'd like to exclude two of my printers from this script because they are virtual queues that hold jobs until they're released for up to 180 minutes. Does anyone know how to add an exclusion rule to this script?

Thanks to anyone that might be able to help me out!


Notes:
My virtual printers are called "Find Me Printer" and "Find Me Printer (color)".
I'm using a Window 2008 R2 Print Server
MontserratITAsked:
Who is Participating?
 
Steve WhitcherSystems AdministratorCommented:
You should be able to filter those out by adding them to the existing Where-Object command.  For example:

$PrintJobs = Get-WmiObject Win32_PrintJob | Where-Object { ($_.Name -notlike "Find Me Printer*") -and ([System.Management.ManagementDateTimeConverter]::ToDateTime($_.TimeSubmitted) -lt $($(Get-Date).addMinutes(-2)))}

foreach ($job in $PrintJobs) 
{    
   "Canceling job $($job.JobId)"   
   $job.Delete() 
}

Open in new window


Assuming those are the only 2 print queues you have with names that begin with "Find Me Printer", that should work.

Also, note that the script you copied from the other thread didn't actually cancel the job, it just selected the jobs and then printed a message to the screen that said it was cancelling the job.  The OP's script in that thread did included the call to $job.Delete() which would actually delete the job, and I added it back in to the script above.
1
 
MontserratITAuthor Commented:
Thank you so much!
0
 
MontserratITAuthor Commented:
Would anyone know add to add a line to check for a null value before attempting to delete the job?

The script will spit out an error if there are no print jobs to cancel. I'm hoping to schedule this as a task.
0
How do you know if your security is working?

Protecting your business doesn’t have to mean sifting through endless alerts and notifications. With WatchGuard Total Security Suite, you can feel confident that your business is secure, meaning you can get back to the things that have been sitting on your to-do list.

 
Steve WhitcherSystems AdministratorCommented:
Not at my desk to test it right now, but you should be able to insert this on line 2, before the foreach:
If ($printjobs) {

Open in new window


And then put a } at the end of the script.
0
 
Steve WhitcherSystems AdministratorCommented:
So, the end result might look like:
$PrintJobs = Get-WmiObject Win32_PrintJob | Where-Object { ($_.Name -notlike "Find Me Printer*") -and ([System.Management.ManagementDateTimeConverter]::ToDateTime($_.TimeSubmitted) -lt $($(Get-Date).addMinutes(-2)))}

if ($Printjobs) 
{
    foreach ($job in $PrintJobs) 
        {    
            "Canceling job $($job.JobId)"   

        }
}

Open in new window

0
 
MontserratITAuthor Commented:
Thanks I'll give that a shot. I'll add that last line in too so it looks like this:

$PrintJobs = Get-WmiObject Win32_PrintJob | Where-Object { ($_.Name -notlike "Find Me Printer*") -and ([System.Management.ManagementDateTimeConverter]::ToDateTime($_.TimeSubmitted) -lt $($(Get-Date).addMinutes(-2)))}

if ($Printjobs) 
{
    foreach ($job in $PrintJobs) 
        {    
            "Canceling job $($job.JobId)"   
			$job.Delete() 
        }
}

Open in new window

1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.