-
Notifications
You must be signed in to change notification settings - Fork 1
/
PrintServerToolkit.psm1
193 lines (155 loc) · 6.62 KB
/
PrintServerToolkit.psm1
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
function Restore-SpoolerService {
<#
.SYNOPSIS
Restarts Window's print spooler service. Optionally, removes items in the spooler cache and/or performs validation.
.DESCRIPTION
Restarts Window's print spooler service.
Optionally, deletes all files in the spooler cache in C:\Windows\System32\spool\PRINTERS.
Optionally, performs validation and returns true or false.
.PARAMETER DeleteCache
Switch: Deletes all files within the Windows spooler cache located here: C:\Windows\System32\spool\PRINTERS.
.PARAMETER ReturnResult
Switch: Performs validation and returns a boolean value for success or failure.
.INPUTS
None. You cannot pipe objects to Restore-SpoolerService.
.OUTPUTS
System.Boolean. ResturnResult switch returns a boolean value for success or failure.
.EXAMPLE
Restore-SpoolerService -ReturnResult -Verbose
VERBOSE: Stopped the spooler service.
VERBOSE: Starting the spooler service.
VERBOSE: Success: Spooler service is running.
True
.EXAMPLE
Restore-SpoolerService -ReturnResult -DeleteCache -Verbose
VERBOSE: Stopped the spooler service.
VERBOSE: Removing items from spooler cache.
VERBOSE: Starting the spooler service.
VERBOSE: Success: Spooler cache is empty and the spooler service is running.
.LINK
Github source: https://github.com/SnoozingPinata/PrintServerToolkit
.LINK
Author's website: www.samuelmelton.com
#>
[CmdletBinding()]
Param (
[Parameter()]
[Switch]$DeleteCache,
[Parameter()]
[Switch]$ReturnResult
)
Begin {
}
Process {
# Stopping the service.
Stop-Service -Name Spooler -Force
# Checking to see if service has stopped, throws errors if not.
if ((Get-Service -Name Spooler).Status -ne "Stopped") {
throw "Failed to stop the Spooler service. Verify you are running this command as an administrator."
}
Write-Verbose "Stopped the spooler service."
# If the DeleteCache switch was used, this deletes everything in the print spooler cache.
if ($DeleteCache) {
Write-Verbose "Removing items from spooler cache."
Get-ChildItem -Path "C:\Windows\System32\spool\PRINTERS" | ForEach-Object -Process {
Remove-Item -Path $_.PSPath -Force
Write-Verbose -Message "Removed $($_.PSPath)"
}
}
# Tries to start spooler service 3 times before continuing.
[int] $startServiceTryCount = 0
do {
Write-Verbose "Starting the spooler service."
try {
Start-Service -Name Spooler
}
catch {
Write-Verbose "Failed to start the Spooler service. Waiting 10 seconds to try again."
Start-Sleep -Seconds 10
}
$startServiceTryCount += 1
} while (((Get-Service -Name Spooler).Status -eq "Stopped") -and ($startServiceTryCount -lt 3))
# Performs validation. Returns true or false if ReturnResult switch was used. Different set of checks if DeleteCache switch was also used.
if ($ReturnResult){
$finalServiceStatus = (Get-Service -Name Spooler).status
# This is the code for the return value if DeleteCache switch was used.
if ($DeleteCache){
$finalCacheCount = (Get-ChildItem -Path "C:\Windows\System32\spool\PRINTERS").count
if (($finalCacheCount) -eq 0 -and ($finalServiceStatus -eq "Running")) {
Write-Verbose "Success: Spooler cache is empty and the spooler service is running."
Return $true
} else {
Write-Verbose "Failure:`nRemaining items in cache: $($finalCacheCount)`nService status: $($finalServiceStatus)"
Return $false
}
}
# This is the code for the return value if ReturnResult switched was used, but DeleteCache was not.
if ($finalServiceStatus -eq "Running"){
Write-Verbose "Success: Spooler service is running."
Return $true
} else {
Write-Verbose "Failure: Spooler service is $($finalServiceStatus)"
Return $false
}
}
}
End {
}
}
function Remove-UnusedPrinterPorts {
<#
.SYNOPSIS
Removes non-local printer ports that are no longer in use.
.DESCRIPTION
Removes any printer port that is not a "local port" and that is not currently being used by a configured printer.
.INPUTS
None. You cannot pipe objects to Remove-UnusedPrinterPorts.
.OUTPUTS
None. This command does not provide any output.
.EXAMPLE
Remove-UnusedPrinterPorts
.LINK
Github source: https://github.com/SnoozingPinata/PrintServerToolkit
.LINK
Author's website: www.samuelmelton.com
#>
[CmdletBinding()]
Param (
)
Begin {
}
Process {
# Creates empty arrays to compare.
$usedPrinterPorts = @()
$allNonLocalPrinterPorts = @()
# Gets all of the printer ports that are not local ports and adds each one to the $allNonLocalPrinterPorts array.
Get-PrinterPort | Where-Object {$_.Description -ne "Local Port"} | ForEach-Object -Process {
$allNonLocalPrinterPorts += $_.Name
}
# Gets all printers then adds whatever ports they are currently using to $usedPrinterPorts array.
Get-Printer | ForEach-Object -Process {
$usedPrinterPorts += $_.PortName
}
# Sorts both arrays by name ascending.
$usedPrinterPorts = $usedPrinterPorts | Sort-Object
$allNonLocalPrinterPorts = $allNonLocalPrinterPorts | Sort-Object
$errorsOccurred = $false
# Compares the arrays. If a port is on the $allNonLocalPrinterPorts list but not on the $usedPrinterPorts list, the port is removed.
Compare-Object -ReferenceObject $allNonLocalPrinterPorts -DifferenceObject $usedPrinterPorts | ForEach-Object -Process {
if($_.SideIndicator -eq '<=' ){
try {
Remove-PrinterPort -Name $_.InputObject
}
catch{
$errorsOccurred = $true
Write-Verbose "Failed to remove port $($_)"
}
}
}
if ($errorsOccurred) {
throw "Failed to remove one or more ports."
}
}
End {
}
}