适用于Windows 8.1或Server 2012 R2
Windows 8.1 和 Server 2012 R2上自带了一个PowerShell组件:“PrintManagement“,它包含了所有管理本机和远程打印机的命令。
在之前的小技巧中,我们演示了如何读取打印机任务。每一个打印任务都有一个属性JobStatus ,用来表示该任务是否打印成功。
所有的状态可以这样获取:
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
接下来就可以过滤已存在的任务了。比如你想列出打印任务是否已经完成,或者遇到了故障:
$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即可:
$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