-
Notifications
You must be signed in to change notification settings - Fork 0
/
Restart-FailedServiceRecovery.ps1
52 lines (41 loc) · 1.3 KB
/
Restart-FailedServiceRecovery.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
$i = 0
$Seconds = 5
$RetryCount = 3
$ServiceName = 'Print Spooler'
$scomApi=New-Object -ComObject Mom.ScriptApi
$InformationValue = 0
$ErrorValue = 1
$WarningValue =2
$ScriptName = "Retry-FailedService.ps1"
$SuccessEventID = 1982
$ErrorEventID =1983
$DisabledEventID=1984
do
{
try
{
$Service = Get-Service -DisplayName $ServiceName -ErrorAction Stop
if ($Service.StartType -ne 'Disabled')
{
$Service | Start-Service -ErrorAction Stop
$Message = "[$ServiceName] is started after $i retries."
$scomApi.LogScriptEvent($ScriptName,$SuccessEventID,$InformationValue,$Message)
$Message
}
else
{
$Message = "[$ServiceName] is disabled not retrying.."
$scomApi.LogScriptEvent($ScriptName,$DisabledEventID,$InformationValue,$Message)
$Message
}
}
catch
{
$i+=1
$Message = "[$ServiceName] `nRetry Count: $i`nError Occured. Error: $($Error[0].Exception.Message).`nWill retry $RetryCount times.`nSleeping $Seconds seconds."
$scomApi.LogScriptEvent($ScriptName,$ErrorEventID,$ErrorValue,$Message)
$Message
Start-Sleep -Seconds $Seconds
}
}
until ($i -eq $RetryCount -or $Service.Status -eq 'Running' -or $Service.StartType -eq 'Disabled')