Problem
So I have a block of code meant to start a service and have the service time out if it takes too long and works fine for the most part. Unfortunately, when this service attempts to start a service that cannot be started, it will display the following Warning Message:
WARNING: Waiting for 'ServiceName <ServiceName>' to start...
This message will display once every 1-2 seconds, but the time-out never occurs and the Start-Service action seems to continue endlessly. I can safely verify that it will at least continue for 30 minutes (before I manually killed the action).
The code is as follows:
Start-Service $ServiceName -ErrorAction SilentlyContinue
$results = Wait-ServiceState -ServiceName $ServiceName -ServiceState 'Running' -SecondsToWait $SecondsToWait
Function Wait-ServiceState {
Param (...)
Process {
Try {
$check = Get-Service $ServiceName
$timeout = New-TimeSpan -Seconds $SecondsToWait
$check.WaitForStatus($ServiceState, $timeout.ToString())
If ($check.Status -ne $ServiceState) { return $False }
Else { return $true }
}
Catch {
# do nothing: handled by an upper level function
}
}
}
I have tested the above function against a couple other services and it appears to work fine. I have also successfully used it with Stop-Service with absolutely zero issues. I have been searching through threads all morning and have not been able to find anything even remotely similar. I do not know what I am missing...
Question
Does anyone have an idea on why it isn't timing out like its supposed to? Perhaps even know how to resolve this?