locked
Get-Service start-service when service stops automatically restart RRS feed

  • Question

  • The software I am using has a set of services that start and stop in a particular order as one is dependent on the other. These services are stopping by themselves and until I resolve that issue I wanted to write a script that would test the service and then do an If Then to test to see if it stopped and run the start-service command to restart it. I know I could do it simply by looping the start-service command but I wanted a bit more of a challenge. I didn't see any options to be able to assign the "status" to a variable. This is the loop I have. (found an example in this forum)

    While ($true) {

     if (Test-Path ($env:USERPROFILE + "\temp.file")) {
        Break
        } else {
        Start-Sleep -seconds 30

    }

    I would like to add in the test for the status so that service can be redirected to restart. What is the best way to assign the status to a variable? I am using this to see the status...

    get-service -DisplayName * | where-object {$_.status -eq "stopped"}

    Thanks

    Thursday, September 7, 2017 1:22 PM

All replies

  • Hi Sullijwiii 

    If you set get-service as a variable you can then use the variable name like $service and add .status -eq the status you want like below. 

    $service = Get-Service | where {$_.DisplayName -like "Adobe Acrobat Update Service"}
    if ($service.status -eq "Stopped") {
    Start-Service $service
    }
    else
    {
    Write-Host $service.DisplayName " is Running"
    }
    


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.


    Thursday, September 7, 2017 1:45 PM
  • You can configure a service to automatically restart.  Look at th service properties and you will see a number of options that will allow the service to automatically restart. 

    \_(ツ)_/

    Thursday, September 7, 2017 3:47 PM
  • As jrv stated, if you go to the properties of the service, there is a recovery tab you can configure to restart the service. Also, there is a dependency tab, which you can configure to add other services that a service may depend on, and will only start if that service is started first.

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Thursday, September 7, 2017 5:27 PM
  • Hello,

    I've already answer for related question, please check following link for the same

    https://social.technet.microsoft.com/Forums/scriptcenter/en-US/4c984184-6749-4069-83d8-336a923096d3/stopstart-restart-service-on-remote-server?forum=ITCG#005a1bfa-924d-4684-bd7d-9167563da808

    NTRao

    Please mark as answer if it helps!

    • Proposed as answer by NTRao Monday, September 11, 2017 4:44 AM
    Friday, September 8, 2017 7:19 AM