Wait for service to invoke script block

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.
Do
{
$Service = Get-Service -Name $Service
}
Until ($Service.Status -eq “Running”)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s