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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Make Network Traffic Fast and Furious with SD-WAN

Software-defined WAN (SD-WAN) is a technology that determines the most effective way to route traffic to and from datacenter sites. Register for the webinar today to learn how your business can benefit from SD-WAN!

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.