適用于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