I was recently required to create a wait for process to check if a particular service was running prior to invoking a number of cmdlets of the script block, which had a dependency on this service being in a ‘running’ state.
This is relatively simple to achieve using the Do statement in a inverted loop to evaluate if the service status is returned as ‘running’.
Firstly, the script will require the service name to be evaluated to be set as a variable.
# Specify the service name as a parameter
$Service = “” # Specify name property of the service.
Then, the script will then use the Do statement to check the status of the service specified in the variable until the service status is running and then exit the loop and allow the remaining script block to be invoked.
# Gets the status of the service name until the service status is returned as running.
$Service = Get-Service -Name $Service
Until ($Service.Status -eq “Running”)