I need to start an ordered sequence of services, i need that each service will be up and running before try to start the next one, how can I achieve this in Powershell? How can I wait for the stop too?
Thanks,
DD
If you have a list of service names (say in an array), then foreach service:
The key is likely to be handling all the possibilities for #3 including the service failing.
But an outline would be something like (without handling the error cases):
$serviceNames | Foreach-Object -Process {
$svc = Get-Service -Name $_
if ($svc.Status -ne 'Running') {
$svc.Start()
while ($svc.Status -ne 'Running') {
Write-Output "Waiting for $($svc.Name) to start, current status: $($svc.Status)"
Start-Sleep -seconds 5
}
}
Write-Output "$($svc.Name) is running"
}
Get-Service
returns an instance of System.ServiceProcess.ServiceController
which is "live"—indicating the current state of the service, not just the state when the instance was created.
A similar stop process would replace "Running" with "Stopped" and the "Start" call with "Stop". And, presumably, reverse the order of the list of services.