title: How do I start multiple services in a specific order and if there is no service available, it will skip to the next available one?
body: <p>This script will find the services listed in the variables set.  If the service is not running, it will start the service.  It also checks and starts the service in a specific order.  It also reads the servers from a text file.</p>
  
  <p>However, the question here is how do you make it so that if the service is not available, it will skip to the next available service?  Currently, the script just stops when there is no service available.</p>
  
  <pre><code>Function startOTCSServices {
  
  $ComputerPath = "C:\PS Scripts\OTCS CSIRT\servernames.otcs.txt"
  $OTCSServicePath = "KIC-ED-MC02"
  $OTCSServicePath1 = "KIC-ED-MC"
  $OTCSServicePath2 = "Ascent Capture Service"
  $OTCSServicePath3 = "ACIS Remote Synchronization Agent"
  
  try {
      $ComputerNames = Get-Content -Path $ComputerPath  
  
      if ($ComputerNames -ne $null) 
      {    
  
          foreach ($c in $ComputerNames) 
          {
              Write-Host "Server:" $c -ForegroundColor Yellow
              Write-Host "-------------------------------------------"
              Write-Host "Starting Services"
              Write-Host "-------------------------------------------"
  
              $p = Get-Service -Name $OTCSServicePath -ComputerName $c -ErrorAction SilentlyContinue
              $p1 = Get-Service -Name $OTCSServicePath1 -ComputerName $c -ErrorAction SilentlyContinue
              $p2 = Get-Service -Name $OTCSServicePath2 -ComputerName $c -ErrorAction SilentlyContinue
              $p3 = Get-Service -Name $OTCSServicePath3 -ComputerName $c -ErrorAction SilentlyContinue
  
                  while ($p.Status -ne "Running") 
                  {              
                      Start-Service $p -Verbose                          
                      Start-Sleep -s 5
                      $p.Refresh() 
                      if ($p.Status -eq "Running") 
                      {
                          Write-Host $p "is now started" -ForegroundColor Green 
                      }
                  }
                  while ($p1.Status -ne "Running") 
                  {   
                      Start-Service $p1 -Verbose                          
                      Start-Sleep -s 5
                      $p1.Refresh() 
                      if ($p1.Status -eq "Running") 
                      {
                          Write-Host $p1 "is now started" -ForegroundColor Green
                      }
                  }
                  while ($p2.Status -ne "Running") 
                  {   
                      Start-Service $p2 -Verbose                          
                      Start-Sleep -s 5
                      $p2.Refresh() 
                      if ($p2.Status -eq "Running") 
                      {
                          Write-Host $p2 "is now started" -ForegroundColor Green
                      }
                  }
                  while ($p3.Status -ne "Running") 
                  {   
                      Start-Service $p3 -Verbose                          
                      Start-Sleep -s 5
                      $p3.Refresh() 
                      if ($p3.Status -eq "Running") 
                      {
                          Write-Host $p3 "is now started" -ForegroundColor Green
                      }
                  }
           }             
      } else { Write-Host "No computers found in file" }
       } catch { Write-Host $_ }
  </code></pre>
  
  <p>}</p>