-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_queue_viewer.ps1
129 lines (114 loc) · 4.28 KB
/
print_queue_viewer.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
function Show-PrinterMenu {
Clear-Host
Write-Host "=== Printer Queue Viewer ===" -ForegroundColor Cyan
Write-Host
# Get all printers including network printers
$printers = Get-Printer | Sort-Object Name
# Display printer list with numbers
for ($i = 0; $i -lt $printers.Count; $i++) {
$status = $printers[$i].PrinterStatus
$type = if ($printers[$i].Type -eq "Connection") { "Network" } else { "Local" }
Write-Host ("{0,3}) {1,-40} [{2}] - {3}" -f ($i + 1), $printers[$i].Name, $type, $status)
}
Write-Host
Write-Host "Q) Quit" -ForegroundColor Yellow
Write-Host
# Get user selection
do {
$selection = Read-Host "Select a printer (1-$($printers.Count) or Q to quit)"
if ($selection -eq 'Q') {
return $null
}
} while (
-not ($selection -match '^\d+$') -or
[int]$selection -lt 1 -or
[int]$selection -gt $printers.Count
)
return $printers[$selection - 1]
}
function Show-PrintQueue {
param (
[Parameter(Mandatory = $true)]
$Printer
)
while ($true) {
Clear-Host
Write-Host "=== Print Queue for $($Printer.Name) ===" -ForegroundColor Cyan
Write-Host
# Get print jobs for the selected printer
$jobs = Get-PrintJob -PrinterName $Printer.Name | Sort-Object JobStatus, SubmittedTime
if ($jobs.Count -eq 0) {
Write-Host "No jobs in queue" -ForegroundColor Yellow
Write-Host
Write-Host "Options:" -ForegroundColor Green
Write-Host "B) Back to printer selection"
Write-Host "Q) Quit"
$choice = Read-Host "Select an option"
switch ($choice.ToUpper()) {
'B' { return }
'Q' { exit }
}
}
else {
# Display all jobs
$jobList = @()
for ($i = 0; $i -lt $jobs.Count; $i++) {
$job = $jobs[$i]
Write-Host ("{0,3}) Job ID: {1}" -f ($i + 1), $job.JobId)
Write-Host (" Document: {0}" -f $job.DocumentName)
Write-Host (" Status: {0}" -f $job.JobStatus)
Write-Host (" Submitted: {0}" -f $job.SubmittedTime)
Write-Host (" Pages: {0}" -f $job.PagesPrinted)
Write-Host ("-" * 40)
$jobList += $job
}
Write-Host
Write-Host "Options:" -ForegroundColor Green
Write-Host "1-$($jobs.Count)) Cancel specific job"
Write-Host "A) Cancel ALL jobs" -ForegroundColor Red
Write-Host "R) Refresh queue"
Write-Host "B) Back to printer selection"
Write-Host "Q) Quit"
Write-Host
$choice = Read-Host "Select an option"
switch -Regex ($choice.ToUpper()) {
'^[0-9]+$' {
$jobIndex = [int]$choice - 1
if ($jobIndex -ge 0 -and $jobIndex -lt $jobs.Count) {
$jobToCanel = $jobList[$jobIndex]
try {
Remove-PrintJob -InputObject $jobToCanel
Write-Host "Job cancelled successfully!" -ForegroundColor Green
}
catch {
Write-Host "Error cancelling job: $_" -ForegroundColor Red
}
Start-Sleep -Seconds 2
}
}
'A' {
try {
$jobs | ForEach-Object { Remove-PrintJob -InputObject $_ }
Write-Host "All jobs cancelled successfully!" -ForegroundColor Green
}
catch {
Write-Host "Error cancelling jobs: $_" -ForegroundColor Red
}
Start-Sleep -Seconds 2
}
'R' { continue }
'B' { return }
'Q' { exit }
}
}
}
}
# Main loop
while ($true) {
$selectedPrinter = Show-PrinterMenu
if ($null -eq $selectedPrinter) {
break
}
Show-PrintQueue -Printer $selectedPrinter
}
Write-Host "Goodbye!" -ForegroundColor Yellow