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

Welcome Forums General PowerShell Q&A Check the status of a particular service and restart it if stopped

This topic contains 5 replies, has 4 voices, and was last updated by

 
Participant
3 weeks, 1 day ago.

  • Author
    Posts
  • #158282

    Participant
    Topics: 4
    Replies: 6
    Points: 47
    Rank: Member

    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

  • #158286

    Participant
    Topics: 3
    Replies: 264
    Points: 78
    Helping Hand
    Rank: Member
    If ((Get-Service -Name ServiceName).Status -eq 'Running'){
    Write-Verbose "ServiceName is running" -Verbose}Else{
    Start-Service -Name ServiceName -Verbose}
    
    • #158288

      Participant
      Topics: 4
      Replies: 6
      Points: 47
      Rank: Member

      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: (:) [Get-Service], ParameterBindingException
      + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetServiceCommand

    • #158295

      Participant
      Topics: 0
      Replies: 35
      Points: 354
      Helping Hand
      Rank: Contributor

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

    • #158289

      Participant
      Topics: 4
      Replies: 6
      Points: 47
      Rank: Member

      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: (:) [Get-Service], ParameterBindingException
      + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetServiceCommand

  • #158301

    Participant
    Topics: 2
    Replies: 957
    Points: 1,730
    Helping Hand
    Rank: Community Hero

    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

You must be logged in to reply to this topic.