Check the status of a particular service and restart it if stopped

Hi There,

I am looking for help in writing a PS script to check the status of a particular windows service and restart it if stopped. Any help is greatly appreciated.

Thank You

Jeevan

If ((Get-Service -Name ServiceName).Status -eq 'Running'){
Write-Verbose "ServiceName is running" -Verbose}Else{
Start-Service -Name ServiceName -Verbose}

I get the below error

Get-Service : A positional parameter cannot be found that accepts argument ‘Client’.
At line:1 char:6

  • If ((Get-Service -Name Tanium Client ).Status -eq ‘Running’){
  •  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidArgument: (:slight_smile: [Get-Service], ParameterBindingException
    • FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetServiceCommand

Get-Service : A positional parameter cannot be found that accepts argument ‘Client’.
At line:1 char:6

  • If ((Get-Service -Name Tanium Client ).Status -eq ‘Running’){
  •  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidArgument: (:slight_smile: [Get-Service], ParameterBindingException
    • FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetServiceCommand

You need to put ‘Tanium Client’ in quotes. it’s seeing client as a parameter instead of the name of the service.

Why script this?
This what GP/GPP (Group Policy / Group Policy Preferences) is for.
See the screen shot here:
https://superuser.com/questions/686398/group-policy-restart-specific-service-if-it-stops