-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSet-ServiceRecovery.ps1
More file actions
184 lines (144 loc) · 7.68 KB
/
Set-ServiceRecovery.ps1
File metadata and controls
184 lines (144 loc) · 7.68 KB
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
function Set-ServiceRecovery {
<#
.SYNOPSIS
Set the Service Recovery options
.DESCRIPTION
Set the Service Recovery options
.NOTES
File-Name: Set-ServiceRecovery.ps1
Author: Josh Burkard - josh@burkard.it
Version: 1.0.0
Changelog:
1.0.0, 2024-10-17, Josh Burkard, initial creation
.PARAMETER ComputerName
the name of a remote computer
this string parameter is not mandatory, and use the local computer if not defined
.PARAMETER ServiceName
the name of service to set (not the Display Name)
this string parameter is mandatory
.PARAMETER action1
the action which is executed when the service stops the first time
this parameter allows this values:
- none
- reboot --> restart the computer
- restart --> restart the service
- run --> runs a command
this string parameter is not mandatory, if not defined 'restart' will be used.
.PARAMETER action2
the action which is executed when the service stops the second time
this parameter allows this values:
- none
- reboot --> restart the computer
- restart --> restart the service
- run --> runs a command
this string parameter is not mandatory, if not defined 'restart' will be used.
.PARAMETER actionLast
the action which is executed when the service stops the third or more time
this parameter allows this values:
- none
- reboot --> restart the computer
- restart --> restart the service
- run --> runs a command
this string parameter is not mandatory, if not defined 'restart' will be used.
.PARAMETER time1
the timeout in milliseconds to start the service the first time
this string parameter is not mandatory, if not defined '30000' will be used.
.PARAMETER time2
the timeout in milliseconds to start the service the second time
this string parameter is not mandatory, if not defined '30000' will be used.
.PARAMETER timeLast
the timeout in milliseconds to start the service the third or more time
this string parameter is not mandatory, if not defined '30000' will be used.
.PARAMETER resetcounter
this parameter defines the Length of period of no failures (in seconds)
after which to reset the failure count to 0
this string parameter is not mandatory, if not defined '4000' will be used.
.PARAMETER command
this parameter defines the command to execute
this string parameter is mandatory if one of the 3 actions has a 'run' definied. this parameter is only in this case visible
.PARAMETER RebootMessage
this parameter defines the message to display before a reboot
this string parameter is mandatory if one of the 3 actions has a 'reboot' definied. this parameter is only in this case visible
.LINK
https://github.com/joshburkard/___PowerShell-Functions
.EXAMPLE
Set-ServiceRecovery -ServiceName sshd -action1 restart -action2 none -actionLast none
#>
[CmdletBinding()]
param
(
[string] [Parameter(Mandatory=$true)] $ServiceName,
[string] [Parameter(Mandatory=$false)] $ComputerName = $env:COMPUTERNAME,
[ValidateSet('run','restart','reboot','none')]
[string] $action1 = "restart",
[int] $time1 = 30000, # in miliseconds
[ValidateSet('run','restart','reboot','none')]
[string] $action2 = "restart",
[int] $time2 = 30000, # in miliseconds
[ValidateSet('run','restart','reboot','none')]
[string] $actionLast = "restart",
[int] $timeLast = 30000, # in miliseconds
[int] $resetCounter = 4000 # in seconds
#,
#[string]$command
# ,
# [string]$RebootMessage
)
DynamicParam {
$paramDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
# $attributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
if ( 'run' -in @( $action1, $action2, $actionLast ) ) {
$commandAttribute = New-Object System.Management.Automation.ParameterAttribute
# $commandAttribute.Position = 3
$commandAttribute.Mandatory = $true
$commandAttribute.HelpMessage = "Please enter the command to execute:"
$attributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
$attributeCollection.Add($commandAttribute)
$commandParam = New-Object System.Management.Automation.RuntimeDefinedParameter('command', [string], $attributeCollection)
$paramDictionary.Add('command', $commandParam)
}
if ( 'reboot' -in @( $action1, $action2, $actionLast ) ) {
$rebootMessageAttribute = New-Object System.Management.Automation.ParameterAttribute
# $commandAttribute.Position = 3
$rebootMessageAttribute.Mandatory = $true
$rebootMessageAttribute.HelpMessage = "Please enter the RebootMessage:"
$attributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
$attributeCollection.Add($rebootMessageAttribute)
$commandParam = New-Object System.Management.Automation.RuntimeDefinedParameter('RebootMessage', [string], $attributeCollection)
$paramDictionary.Add('RebootMessage', $commandParam)
}
return $paramDictionary
}
Process {
if ( ( 'reboot' -in @( $action1, $action2, $actionLast ) ) -and ( -not [boolean]$PSBoundParameters.RebootMessage ) ) {
throw "a reboot message must be defined when reboot action is used"
}
if ( ( 'run' -in @( $action1, $action2, $actionLast ) ) -and ( -not [boolean]$PSBoundParameters.command ) ) {
throw "a command must be defined when run action is used"
}
$serverPath = "\\" + $ComputerName
$services = Get-CimInstance -ClassName 'Win32_Service' | Where-Object { $_.Name -imatch $ServiceName }
foreach ( $service in $services ) {
$ArgumentList = "$serverPath failure $($service.Name) actions= "
<#
if ( $action1 -eq 'none' ) { $action1_c = '""' } else { $action1_c = $action1 }
if ( $action2 -eq 'none' ) { $action2_c = '""' } else { $action2_c = $action2 }
if ( $actionLast -eq 'none' ) { $actionLast_c = '""' } else { $actionLast_c = $actionLast }
#>
$ArgumentList += $action1+"/"+$time1
$ArgumentList += "/"+$action2+"/"+$time2
$ArgumentList += "/"+$actionLast+"/"+$timeLast
# $action = $action1+"/"+$time1+"/"+$action2+"/"+$time2+"/"+$actionLast+"/"+$timeLast
if ( [boolean]$PSBoundParameters.command ) {
$ArgumentList += " command= ""$( $PSBoundParameters.command )"""
}
if ( [boolean]$PSBoundParameters.RebootMessage ) {
$ArgumentList += " reboot= ""$( $PSBoundParameters.RebootMessage )"""
}
$ArgumentList += " reset= $resetCounter"
# https://technet.microsoft.com/en-us/library/cc742019.aspx
# $output = sc.exe $serverPath failure $($service.Name) actions= $action reset= $resetCounter
Start-ProcessAdv -FilePath 'C:\Windows\System32\sc.exe' -ArgumentList $ArgumentList
}
}
}