title: PowerShell different services to run in particular sequence (Remotely)
body: <p>First post! I apologize in advance for formatting. I'm just getting familiar with PowerShell and I'm wanting to Stop a service first, restart another service, and start the initial service. Before moving onto the next next service, I want to make sure that the service has stopped before proceeding.</p>
  <p>I'm using this function that was mentioned <a href="https://stackoverflow.com/questions/28186904/powershell-wait-for-service-to-be-stopped-or-started">here</a> and tried to tailor it for my code.</p>
  <p>Workflow Goal:</p>
  <ol>
  <li>Stop Service A</li>
  <li>Restart Service B</li>
  <li>Start Service A</li>
  </ol>
  <p>Code:</p>
  <pre><code>#Stops Service A and validates its in &quot;Stopped&quot; status
  Get-Service 'ServiceNameA' -ComputerName 'ExampleServerA' | Stop-Service -force -PassThru
  
  function WaitUntilServices1($searchString, $status)
  {
      # Get all services where DisplayName matches $searchString and loop through each of them.
      foreach($service in (Get-Service -DisplayName $searchString))
      {
          # Wait for the service to reach the $status or a maximum of 30 seconds
          $service.WaitForStatus($status, '00:00:30')
      }
  }
  
  WaitUntilServices1 &quot;ServiceDisplayNameA&quot; &quot;Stopped&quot;
  
  #Restarts Service B and validates its in &quot;Running&quot; status
  Get-Service 'ServiceNameB' -ComputerName 'ExampleServerB' | Restart-Service -force -PassThru
  
  function WaitUntilServices2($searchString, $status)
  {
      # Get all services where DisplayName matches $searchString and loop through each of them.
      foreach($service in (Get-Service -DisplayName $searchString))
      {
          # Wait for the service to reach the $status or a maximum of 30 seconds
          $service.WaitForStatus($status, '00:00:30')
      }
  }
  
  WaitUntilServices2 &quot;ServiceDisplayNameB&quot; &quot;Running&quot;
  
  #Start Service A and validates its in &quot;Running&quot; status
  Get-Service 'ServiceA' -ComputerName 'ExampleServerA' | Start-Service -force -PassThru
  
  Read-Host -Prompt &quot;Press Enter to exit&quot; 
  </code></pre>
  <p>The Code I have above is giving me the following Errors for both of the functions.</p>
  <pre><code>Exception calling &quot;WaitForStatus&quot; with &quot;2&quot; argument(s): &quot;Time out has expired and the operation has not been completed.&quot; At C:\PowerShell\ScriptExample\ScriptExampleFix.ps1:10 char:9
  $service.WaitForStatus($status, '00:00:30')
  
  + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
  + FullyQualifiedErrorId : TimeoutException
  </code></pre>
  <p>Then for the very last portion to start the service I'm getting 1 more error:</p>
  <pre><code>Start-Service : A parameter cannot be found that matches parameter name 'force'. At C:\PowerShell\ScriptExample\ScriptExampleFix.ps1:32 char:85
  + ... erName 'ServerNameExample' | Start-Service -force -PassTh ...
  +                                                            ~~~~~~
  + CategoryInfo          : InvalidArgument: (:) [Start-Service], ParameterBindingException
  + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.StartServiceCommand
  </code></pre>
  <p>Any help would get greatly appreciated :)</p>