-
Notifications
You must be signed in to change notification settings - Fork 1
/
Set-ServiceRecoveryOptions-SC-Wrapper.ps1
258 lines (110 loc) · 5.74 KB
/
Set-ServiceRecoveryOptions-SC-Wrapper.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
####################################################################################
#.Synopsis
# This script is a wrapper for SC.EXE to modify service recovery options.
#
#.Description
# Windows services can be configured with recovery options in the event of
# a service failure (see the Services tool, Recovery tab of a service).
# This script is a wrapper around SC.EXE to help configure the recovery
# options for all the service names specified in -ServicesList on the local
# or a remote computer.
#
#.Parameter ComputerName
# Name of the remote computer. Defaults to LocalHost.
#
#.Parameter ServicesList
# An array of service names or the path to a file containing service names.
# These are the internal service names, not the Display Names of the services.
# These are the services whose recovery options will be reconfigured.
# Defaults to ServicesList.txt in the present working directory.
#
#.Parameter Seconds
# The number of seconds after which the count of service failures is reset
# to zero. The default is 259200, which is 3 days.
#
#.Parameter Action1
# The action for the first failure. Must be 'run', 'restart' or 'reboot' only.
# If 'run', then a -RunCommand is required. Defaults to restart.
#
#.Parameter Action2
# The action for the second failure. Must be 'run', 'restart' or 'reboot' only.
# If 'run', then a -RunCommand is required. Defaults to restart.
#
#.Parameter Action3
# The action for the third failure. Must be 'run', 'restart' or 'reboot' only.
# If 'run', then a -RunCommand is required. Defaults to restart.
#
#.Parameter RunCommand
# If an action is to run a command, this is the full command line to run,
# including any command-line arguments.
#
#.Parameter ActionDelay
# The number of milliseconds to pause before each failure action is
# executed. The default is 120000, which is 2 minutes.
#
#.Example
# .\Set-ServiceRecoveryOptions.ps1
#
# Sets all three recovery actions to 'restart' for the services found in
# the ServicesLists.txt file in the present working directory.
#
#.Example
# .\Set-ServiceRecoveryOptions.ps1 -ComputerName "SERVER47"
#
# Sets all three recovery actions to 'restart' for the services found in
# the ServicesLists.txt file on the remote computer named SERVER47.
#
#.Example
# .\Set-ServiceRecoveryOptions.ps1 -ServicesList $list -Action3 "run" -RunCommand "powershell.exe \\server\share\script.ps1"
#
# Sets the first two recovery actions to 'restart' and the third action to 'run'
# for the services specified by $list, where $list could either be the path to a text
# file or an array of service names. Because the third action is 'run', the
# run command must be given too.
#
#
#Requires -Version 2.0
#
#.Notes
# Author: Jason Fossen, Enclave Consulting (http://www.sans.org/windows-security/)
# Version: 1.0
# Updated: 24.Nov.2012
# Legal: 0BSD.
####################################################################################
Param ($ComputerName = $env:computername, $ServicesList = ".\ServicesList.txt", $Seconds = 259200, $Action1 = "restart", $Action2 = "restart", $Action3 = "restart", $ActionDelay = 120000, $RunCommand = $null)
# Convert $Action* arguments to lowercase.
@($Action1,$Action2,$Action3) | foreach { $_ = $_.trim().tolower() }
# Confirm proper choices for the $Action* arguments.
@($Action1,$Action2,$Action3) | foreach { if ($_ -notin @("run","restart","reboot")){ "`nERROR: An action must be one of 'run','restart' or 'reboot' only!`n" ; exit } }
# Construct the $Actions string. The action is "run", "restart" or "reboot", followed by a forward
# slash and the number of milliseconds before the action is executed. No spaces between items.
# For example, an $actions string might be "restart/120000/run/60000/reboot/1000".
# It would be possible to have three different ActionDelay values, but this script defaults
# to using just one value for all three; feel free to modify it if needed.
$Actions = "$Action1/$ActionDelay/$Action2/$ActionDelay/$Action3/$ActionDelay"
# Test for run actions and construct the two halves of the expression to be executed.
if ($actions -like '*run*' -and $RunCommand -eq $null)
{ "`nERROR: With a run action, a -RunCommand argument must be specified too!`n" ; exit }
elseif ($actions -like '*run*')
{ $expression1 = "$env:windir\system32\sc.exe \\$computername failure " ; $expression2 = " reset= $seconds actions= $actions command= '" + $runcommand + "'" }
else
{ $expression1 = "$env:windir\system32\sc.exe \\$computername failure " ; $expression2 = " reset= $seconds actions= $actions" }
# Parse the text file or array containing the names of the services.
if (($ServicesList.gettype().fullname -eq 'System.String') -or ($ServicesList.gettype().fullname -eq 'System.IO.FileInfo'))
{
$ServicesList = @(get-content -path $ServicesList)
if (-not $?) { "`ERROR: Failed to load $ServicesList `n" ; exit }
}
elseif ($ServicesList.gettype().fullname -ne 'System.Object[]')
{ "`ERROR: -ServicesList must be an array of service names or the path to a file with service names!`n" ; exit }
# Exclude blank and comment lines from the list of service names.
$ServicesList = $ServicesList | foreach { if (($_.trim().length -ne 0) -and ($_ -notlike '#*') -and ($_ -notlike ';*')){ $_ } }
if ($ServicesList.count -eq 0) { "`nERROR: -ServicesList cannot be empty of service names!`n"; exit }
# Execute SC.EXE for each service name, displaying the command first for troubleshooting.
$ServicesList | ForEach `
{
$fullexpression = $expression1 + $_ + $expression2
"`n $fullexpression `n"
invoke-expression -command $fullexpression
}
"`n"