PowerShell Query and delete Print task action Code instance _powershell

Source: Internet
Author: User
Tags foreach

Applies to Windows 8.1 or server R2

Windows 8.1 and Server R2 have a PowerShell component: "Printmanagement", which contains all the commands for managing both native and remote printers.

In the previous tip, we demonstrated how to read a printer task. Each print task has a property jobstatus that indicates whether the task was printed successfully.

All States can be obtained in this way:

Copy Code code as follows:

Ps> Import-module printmanagement

ps> [Microsoft.powershell.cmdletization.generatedtypes.printjob.jobstatus]::getnames ([ Microsoft.PowerShell.Cmdletization.GeneratedTypes.PrintJob.JobStatus])
Normal
Paused
Error
deleting
Spooling
Printing
Offline
Paperout
Printed
Deleted
Blocked
Userintervention
Restarted
Complete
Retained
Renderinglocally

The next step is to filter the existing tasks. For example, do you want to list whether the print task has completed, or has encountered a failure:

Copy Code code as follows:

$ComputerName = $env: ComputerName

Get-printer-computername $ComputerName | Foreach-object {
Get-printjob-printername $_. Name-computername $ComputerName |
Where-object {$_. Jobstatus-eq ' Complete '-or $_. Jobstatus-eq ' Error '-or $_. Jobstatus-eq ' printed '}
}

Deleting a print task is also very simple, remove-printjob:

Copy Code code as follows:

$ComputerName = $env: ComputerName

Get-printer-computername $ComputerName | Foreach-object {
Get-printjob-printername $_. Name-computername $ComputerName |
Where-object {$_. Jobstatus-eq ' Complete '-or $_. Jobstatus-eq ' Error '-or $_. Jobstatus-eq ' printed '}
} |
Remove-printjob-cimsession $ComputerName

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.