-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_wait_for_service.ps1
45 lines (44 loc) · 1.67 KB
/
function_wait_for_service.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
Function wait_for_service {
<#
.NOTES
===========================================================================
Created by: Anders Mikkelsen
Organization: NetIT Services
Twitter: @AMikkelsenDK
===========================================================================
.DESCRIPTION
This function waits for a service to get to a certain state
.PARAMETER ServiceName
The name of a Windows service
.PARAMETER ServiceStatus
The service state you want to wait for
.PARAMETER MaxRetries
How many retries do you want before exiting
.PARAMETER SleepIntervalSec
Seconds between retries
.EXAMPLE
wait_for_service -ServiceName "servicename" -ServiceStatus "Stopped"
.EXAMPLE
wait_for_service -ServiceName "servicename" -ServiceStatus "Running" -MaxRetries 10 -SleepIntervalSec 5
#>
param (
[Parameter(Mandatory=$true)][String]$ServiceName,
[Parameter(Mandatory=$true)][String]$ServiceStatus,
[Parameter(Mandatory=$false)][Integer]$MaxRetries=20,
[Parameter(Mandatory=$false)][Ingeter]$SleepIntervalSec=5
)
## Wait until service is in the correct state ($ServiceStatus)
DO{
$thisTestCase = (Get-Service $ServiceName | Where-Object {$_.Status -eq $thisServiceStatus}).Count
$MaxRetries--
Start-Sleep -Seconds $SleepIntervalSec
}
UNTIL ($thisTestCase -eq 0 -OR $MaxRetries -eq 0)
## Return
IF($MaxRetries -eq 0){
RETURN "Max Retries reached, operation was not completed"
}
ELSE{
RETURN "Service '$ServiceName' is now: $ServiceStatus"
}
}