title: Stop and start service in-order on a remote server
body: <p>I want to write a script to check 4 service on a server if they have status running when we stop them by order and start them on the other hand with delay.</p>
  
  <p>First service must check that it is running and then start with others therefore we use delay.</p>
  
  <pre><code>$serverlist = Get-Content “.\server.txt”
  $servicename4 = "Orbit Utveckling Consul"
  $servicename3 = "Orbit Utveckling Identity"
  $servicename2 = "Orbit Utveckling Service"
  $servicename1 = "Orbit Utveckling Integration"
  $delay = 60 # Ange i sekunder.
  
  foreach ($server in $serverlist) {
      $getservicestatus1 = Get-Service -Name $servicename1 -ComputerName $server
      if ($getservicestatus1 -eq "Running") {
          Set-Service -Name $servicename1 -ComputerName $server -Status stopped
          Write-Host "$server $servicename1 Stoppat!" -ForegroundColor Green
          Write-Host "Väntar i $delay sekunder innan $servicename2 stoppar..."
          $nexttime = (Get-Date).AddSeconds($delay)
          Write-Host "Nästa start sker $nexttime"
          Start-Sleep -Seconds $delay
      }
      $getservicestatus2 = (Get-Service -Name $servicename2 -ComputerName $server).status
      if ($getservicestatus2 -eq "Running") {
          Set-Service -Name $servicename2 -ComputerName $server -Status stopped
          Write-Host "$server $servicename2 Stoppat!" -ForegroundColor Green
          Write-Host "Väntar i $delay sekunder innan nästa server stoppar..."
          $nexttime = (Get-Date).AddSeconds($delay)
          Write-Host "Nästa start sker $nexttime"
          Start-Sleep -Seconds $delay
      }
      $getservicestatus3 = (Get-Service -Name $servicename3 -ComputerName $server).status
      if ($getservicestatus3 -eq "Running") {
          Set-Service -Name $servicename3 -ComputerName $server -Status stopped
          Write-Host "$server $servicename3 Stoppat!" -ForegroundColor Green
          Write-Host "Väntar i $delay sekunder innan nästa server stoppar..."
          $nexttime = (Get-Date).AddSeconds($delay)
          Write-Host "Nästa start sker $nexttime"
          Start-Sleep -Seconds $delay
      }
      $getservicestatus4 = (Get-Service -Name $servicename4 -ComputerName $server).status
      if ($getservicestatus4 -eq "Running") {
          Set-Service -Name $servicename4 -ComputerName $server -Status stopped
          Write-Host "$server $servicename4 Stoppat!" -ForegroundColor Green
          Write-Host "Väntar i $delay sekunder innan nästa server stoppar..."
          $nexttime = (Get-Date).AddSeconds($delay)
          Write-Host "Nästa start sker $nexttime"
          Start-Sleep -Seconds $delay
      }
  }
  
  # Filen startar tjänsten Drift_Cosmic på varje IX3a i txt-filen.
  # Det görs med en delay på ett antal sekunder mellan varje start.
  # Detta är en testfil tills dess att en riktig testkörning
  # har gjorts i Drift.
  Write-Host "$servicename4, $servicename3, $servicename2, $servicename1 startas med $delay sekunders mellanrum."
  
  foreach ($server in $serverlist) {
      #   Skriv ut status på tjänsten i passande färg.
      $getservicestatus4 = (Get-Service -Name $servicename4 -ComputerName $server).status
      if ($getservicestatus4 -eq "Stopped") {
          Set-Service -Name $servicename4 -ComputerName $server -Status Running
          Write-Host "$server $servicename4 Startad!" -ForegroundColor Green
          Write-Host "Väntar i $delay sekunder innan nästa server startas..."
          $nexttime = (Get-Date).AddSeconds($delay)
          Write-Host "Nästa start sker $nexttime"
          Start-Sleep -Seconds $delay
      }
  
      $getservicestatus3 = (Get-Service -Name $servicename3 -ComputerName $server).status
      if ($getservicestatus3 -eq "Stopped") {
          Set-Service -Name $servicename3 -ComputerName $server -Status Running
          Write-Host "$server $servicename3 Startad!" -ForegroundColor Green
          Write-Host "Väntar i $delay sekunder innan nästa server startas..."
          $nexttime = (Get-Date).AddSeconds($delay)
          Write-Host "Nästa start sker $nexttime"
          Start-Sleep -Seconds $delay
      }
  
      $getservicestatus2 = (Get-Service -Name $servicename2 -ComputerName $server).status
      if ($getservicestatus2 -eq "Stopped") {
          Set-Service -Name $servicename2 -ComputerName $server -Status Running
          Write-Host "$server $servicename2 Startad!" -ForegroundColor Green
          Write-Host "Väntar i $delay sekunder innan nästa server startas..."
          $nexttime = (Get-Date).AddSeconds($delay)
          Write-Host "Nästa start sker $nexttime"
          Start-Sleep -Seconds $delay
      }
  
      $getservicestatus1 = (Get-Service -Name $servicename1 -ComputerName $server).status
      if ($getservicestatus1 -eq "Stopped") {
          Set-Service -Name $servicename1 -ComputerName $server -Status Running
          Write-Host "$server $servicename1 Startad!" -ForegroundColor Green
          Write-Host "Väntar i $delay sekunder innan nästa server startas..."
          $nexttime = (Get-Date).AddSeconds($delay)
          Write-Host "Nästa start sker $nexttime"
          Start-Sleep -Seconds $delay
      }
  }
  
  Write-Host "." -ForegroundColor DarkBlue
  Read-Host "Tryck ENTER för att avsluta"
  </code></pre>