Check service status and start if stopped

I recently compiled a script to check a service based on a parameter to check is a service is running and if not start, the parameter value is required to be specified as the service name;

# Gets the service specified in the command line arguement
$Service = Get-Service | Where-Object {$_.Name -eq $args[0]}

# Checks if the service status is stopped and starts the service
If ($Service.Status -eq “Stopped”)
{
Start-Service -Name $Service.Name
}

The above is saved as a powershell script (Start-StoppedService.ps1) and can be invoked from a command line specifying the parameter as below, where the Spooler service is checked;

Start-StoppedService Spooler

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