-
Notifications
You must be signed in to change notification settings - Fork 13
/
Restart-PrintSpooler.ps1
174 lines (124 loc) · 7.38 KB
/
Restart-PrintSpooler.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
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
<#
.SYNOPSIS
Restart-PrintSpooler is a cmdlet created to restart the print spooler whenever a print job failes.
This cmdlet was designed to run automatically.
.DESCRIPTION
The task for this cmdlet is executed when the event log PrintService Error 372, 350, and 314 happens.
Once the event is triggered the print spooler on the server and the computer trying to print are restarted.
This runs best as a task in task scheduler.
.NOTES
Author: Robert H. Osborne
Alias: tobor
Contact: [email protected]
.LINK
https://osbornepro.com
https://writeups.osbornepro.com
https://btpssecpack.osbornepro.com
https://github.com/tobor88
https://gitlab.com/tobor88
https://www.powershellgallery.com/profiles/tobor
https://www.linkedin.com/in/roberthosborne/
https://www.credly.com/users/roberthosborne/badges
https://www.hackthebox.eu/profile/52286
.EXAMPLE
Restart-PrintSpooler -Pattern 'Desktop-' -User 'rob.osborne' -To [email protected] -From [email protected] -SmtpServer mail.smtp2go.com
.EXAMPLE
Restart-PrintSpooler -Pattern 'Desktop-' -User 'rob.osborne' -To [email protected] -From [email protected] -SmtpServer mail.smtp2go.com -Verbose
#>
Function Restart-PrintSpooler {
[CmdletBinding()]
param(
[Parameter(Mandatory=$True,
Position=0,
ValueFromPipeLine=$True,
ValueFromPipeLineByPropertyName=$True)]
[string[]]$Pattern,
[Parameter(Mandatory=$True,
Position=1,
ValueFromPipeLine=$True,
ValueFromPipeLineByPropertyName=$True,
HelpMessage="Enter a the administrators SamAccountName that is being used to Invoke-Command on the remote device attempting to print. Example: 'OP-")]
[string[]]$User,
[Parameter(Mandatory=$True,
Position=2,
ValueFromPipeLine=$True,
ValueFromPipeLineByPropertyName=$True,
HelpMessage="Enter the email address you want to send an alert email to if the print spooler service is down. Example: [email protected]")]
[System.Net.Mail.MailAddress]$To
[Parameter(Mandatory=$True,
Position=3,
ValueFromPipeLine=$True,
ValueFromPipeLineByPropertyName=$True,
HelpMessage="Enter the email address that will send the alert email. Example: [email protected]")]
[System.Net.Mail.MailAddress]$From
[Parameter(Mandatory=$True,
Position=4,
ValueFromPipeLine=$True,
ValueFromPipeLineByPropertyName=$True,
HelpMessage="Enter your SMTP Server to use for sending the email. Example: mail.smtp2go.com")]
[Syste.Net.Mail.MailAddress]$SmtpServer,
[Parameter(Mandatory=$True,
Position=5,
ValueFromPipeLine=$True,
ValueFromPipeLineByPropertyName=$True,
HelpMessage="Enter your domain name. Example: osbornepro.com")]
[string[]]$Domain,
) # End param
$EventID = Get-WinEvent -LogName Microsoft-Windows-PrintService/Admin -MaxEvents 1 | Select-Object -ExpandProperty 'Id'
If ( ($EventID -eq 350) -or ($EventID -eq 314 ) ) {
Write-Verbose "Event ID: $EventID `n`nPerforming print spooler restart on $env:COMPUTERNAME..."
Restart-Service -Name "Print Spooler" -Force
If ( (Get-Service -Name 'Print Spooler').Status -ne 'Running' ) {
Start-Service -Name 'Print Spooler'
$Status = (Get-Service -Name 'Print Spooler').Status
If ($Status -ne 'Running') {
$Body ="Print Spooler service status is in a state that is not running. `nCurrent State: $Status`n`Ensure the service gets back up and running."
Send-MailMessage -To $To -From $From -Subject "Print Spooler Issue on $env:COMPUTERNAME" -SmtpServer $SmtpServer -Priority 'Normal' -Body $Body
Get-Service -Name 'Print Spooler' | Restart-Service -Force
} # End If
} # End If
Else {
Write-Verbose "Successfully restarted print spooler on $env:COMPUTERNAME"
} # End Else
} # End If
If ($EventID -eq 372) {
Write-Verbose "Event ID: $EventID `n`nPerforming print spooler restart on $env:COMPUTERNAME..."
Restart-Service -Name "Print Spooler" -Force
If ( (Get-Service -Name 'Print Spooler').Status -ne 'Running' ) {
Start-Service -Name 'Print Spooler'
$Status = (Get-Service -Name 'Print Spooler').Status
If ($Status -ne 'Running') {
$Body ="Print Spooler service status is in a state that is not running. `nCurrent State: $Status`n`Ensure the service gets back up and running."
Send-MailMessage -To $To -From $From -Subject "Print Spooler Issue on $env:COMPUTERNAME" -SmtpServer $SmtpServer -Priority 'Normal' -Body $Body
Get-Service -Name 'Print Spooler' | Restart-Service -Force
} # End If
} # End If
Else {
Write-Verbose "Successfully restarted print spooler on $env:COMPUTERNAME"
} # End Else
Write-Host "If you do not have a password file you can get something to encrypt your password securely at https://github.com/tobor88/PowerShell/blob/master/Hide-PowerShellScriptPassword.ps1"
$PasswordFile = "C:\Users\Public\Documents\Key.AESpassword.txt"
$KeyFile = "C:\Users\Public\Documents\EncryptionKey"
$Key = Get-Content -Path $KeyFile
$Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content -Path $PasswordFile | ConvertTo-SecureString -Key $Key)
$FailedComputer = (Get-WinEvent Microsoft-Windows-PrintService/Admin -MaxEvents 1 | Select-Object -ExpandProperty 'Message').Split(' ') | Select-String -Pattern $Pattern | Out-String
Set-Variable -Name ComputerName -Value $FailedComputer.Trim()
Invoke-Command -HideComputerName ("$ComputerName" + "$Domain") -ScriptBlock {
Write-Verbose "Event ID: $EventID `n`nPerforming print spooler restart on $env:COMPUTERNAME..."
Restart-Service -Name "Print Spooler" -Force
If ( (Get-Service -Name 'Print Spooler').Status -ne 'Running' ) {
Start-Service -Name 'Print Spooler'
$Status = (Get-Service -Name 'Print Spooler').Status
If ($Status -ne 'Running') {
$Body ="Print Spooler service status is in a state that is not running. `nCurrent State: $Status`n`Ensure the service gets back up and running."
Send-MailMessage -To $To -From $From -Subject "Print Spooler Issue on $env:COMPUTERNAME" -SmtpServer $SmtpServer -Priority 'Normal' -Body $Body
Get-Service -Name 'Print Spooler' | Restart-Service -Force
} # End If
} # End If
Else {
Write-Verbose "Successfully restarted print spooler on $env:COMPUTERNAME"
} # End Else
} -Credential $Cred -UseSSL # End ScriptBlock
} # End If
Write-Verbose "Restarting Print Spoolers completed"
} # End Function Restart-PrintSpooler