Waitforstatus service powershell

10,333

Solution 1

The WaitForStatus method is member of System.ServiceProcess.ServiceController class, not ServiceControllerStatus. For example:

$s = Get-Service spooler
$s.WaitForStatus("Stopped")

You could modify your code to something like this:

$serviceIX3 = Get-Service -Name $servicenameIX3 -ComputerName $server
if($serviceIX3.status -eq "Running") {
    Stop-Service $serviceIX3
    $serviceIX3.WaitForStatus("Stopped")
    Write-Host "$server $servicenameIX3 Sttopad!" -ForegroundColor Green
}

Solution 2

Your $getservicestatus is a System.ServiceProcess.ServiceControllerStatus and not a ServiceController object.

This getservicestatus doesn't have WaitForStatus method.

Try this

$getservice = Get-Service -Name $servicename -ComputerName $server

And now you can use $getservices.WaitForStatus("Stopped")

Write-Output "We stop the service: $servicename"
Stop-Service -Name $servicename
$getservice.WaitForStatus("Stopped")
Write-Output "$servicename Stoped"
Share:
10,333
Am Ba
Author by

Am Ba

Updated on June 04, 2022

Comments

  • Am Ba
    Am Ba almost 2 years

    I have written a script to stop the services on any server! But I get error:

          YMethod invocation failed because[System.ServiceProcess.ServiceControllerStatus] does not contain a method   named 'WaitForStatus'.
        At C:\Admin\Scripts\JBoss_StartStopp\stoppa_alla_IX2 och IX3.ps1:18 char:7
        +       $getservicestatus.WaitForStatus("Stopped")
        +       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : MethodNotFound
    

    When you quit a service on a server, I want to wait until the service is stopped and then go to the next service on the next server!

       $serverlist = Get-Content “.\server.txt”
       $servicename = "JBoss_cambio"
    
       $serverlistIX3 = Get-Content “.\ix3.txt”
       $servicenameIX3 = "Laskopia_Cosmic"
    
    
      foreach ($server in $serverlist) {
    
    $getservicestatus = (Get-Service -Name $servicename -ComputerName $server).status
    if ($getservicestatus -eq "Running") {
      Set-Service -Name $servicename -ComputerName $server -Status Stopped
    
      $getservicestatusIX3.WaitForStatus("Stopped")
    
      Write-Host "$server $servicename Stoppad!" -ForegroundColor Green
      }
    else
      {
      Write-Host "$server $servicename var redan Stopped!" -ForegroundColor Yellow
      }
    }
    
    
       foreach ($server in $serverlistIX3) {
    
    $getservicestatusIX3 = (Get-Service -Name $servicenameIX3 -ComputerName $server).status
    if ($getservicestatusIX3 -eq "Running") {
      Set-Service -Name $servicenameIX3 -ComputerName $server -Status Stopped
      $getservicestatusIX3.WaitForStatus("Stopped")
      Write-Host "$server $servicenameIX3 Sttopad!" -ForegroundColor Green
      }
    else
      {
      Write-Host "$server $servicenameIX3 var redan Stoppad!" -ForegroundColor Yellow
      }
    }
    
    
        Write-Host "." -ForegroundColor DarkBlue
         Read-Host "Tryck ENTER för att avsluta"
    
  • Am Ba
    Am Ba about 6 years
    How can I see that it is checking the status until it stops ?! I want to see it waiting for the stop status.
  • Janne Tuukkanen
    Janne Tuukkanen about 6 years
    You're right. Edited the code to use Stop-Service
  • Am Ba
    Am Ba about 6 years
    what do you mean?!
  • Am Ba
    Am Ba about 6 years
    ow can I see that it is checking the status until it stops ?! I want to see it waiting for the stop status.