locked
PowerShell stop windows service RRS feed

  • Question

  • Hi all,

    I use a PowerShell script to stop a service. However, when the service doesn't stop within 30 seconds I get the message:

    --> "The service did not respond to the start or control request in a timely fashion"

    I know that I can adjust the timeout via the registry (ServicePipeTimeout), but I was wondering if it's possible to write the script without having to change the registry.

    EDIT: I actually changed the setting on one of the servers where it occurs and this doesn't seem to have any effect.

    Script (sorry for not adding all param settings):

    # Step 1: Stop service.
    $buildAosServiceObject = (get-service -ComputerName ${env:computername} -Name $buildAosServiceName)
                    if ($buildAosServiceObject.status -eq 'Running')  # if the AOS is running
                    {
                        Add-Content $logfile "$(Get-Date -Format "yyyyMMdd_HHmm") : Stopping AOS $($ServerInstanceName)."
    
                        $buildAosServiceObject | set-service -StartupType 'Disabled'  # Disable the service to prevent automatic restart.
                        $buildAosServiceObject.Stop() | out-null  # Stop the service, but return immediately.
                        Start-Sleep -Seconds 1
                        $buildAosServiceObject.WaitForStatus('Stopped')  # Wait for the service to completely stop
                        $buildAosServiceObject.Refresh()  # Refresh the current status before checking the status condition      
    
                        Add-Content $logfile "$(Get-Date -Format "yyyyMMdd_HHmm") : AOS $($ServerInstanceName) stopped."
                    }
                    else
                    {
                        Add-Content $logfile "$(Get-Date -Format "yyyyMMdd_HHmm") : AOS $($ServerInstanceName) was already stopped."
                    }


    The script will wait for the service to stop, if it stops within 30 seconds.

    It seems that when starting the service, even when that takes longer than 30 seconds, it doesn't throw this message.

    Thanks in advance.

    KR,

    Dieter






    • Moved by Bill_Stewart Friday, November 20, 2015 8:12 PM This is not "fix script for me" forum
    • Edited by Dieter De Saedeleer Monday, January 4, 2016 12:52 PM
    Friday, September 4, 2015 6:50 AM

Answers

  • Hello,

    To fix this issue I wrapped the above code in a try-catch block. That way, when the initial error shows, I have control over it.

    So I wrote a function that will wait some more for the service to stop or kill the service if it takes too long. This function is called in the catch block.

    function WaitForAOStoStopOrKill
    {
        $stateAos = gwmi Win32_Service -ComputerName ${env:computername} -Filter "Name LIKE '$($buildAosServiceName)'" | select -expand State
        
        if ($stateAos -eq "Stopping")
        {
            $procssAos = Get-Process -Id (gwmi Win32_Service -ComputerName ${env:computername} -Filter "Name LIKE '$($buildAosServiceName)'" | select -expand ProcessId)
            
            if ($procssAos.WaitForExit(300000) -eq $false) # Wait another 5 minutes for the service to really stop
            {
                Write-Host "Waiting for 5 more minutes for status 'Stopped' on AOS $($ServerInstanceName) succeeded. AOS is now stopped."
            }
            else
            {
                try
                {
                    # Kill the process, based on PID in case multiple AOSes have the same display name
                    Write-Host "Killing the AOS, because it won't stop in the expected way."
                    $pid = gwmi Win32_Service -ComputerName ${env:computername} -Filter "Name LIKE '$($buildAosServiceName)'" | select -expand ProcessId
                    Write-Host "Process ID from service $($buildAosServiceName) on $(${env:computername}) is $($pid)"
                    Write-Host "Process from service $($buildAosServiceName) on $(${env:computername}) killed."
                    Invoke-Command -ComputerName ${env:computername} {Stop-Process $args -Force} -ArgumentList $pid
                }
                catch
                {
                    Write-Host "AOS $($sourceServerInstanceName) couldn't be stopped in any way. Script terminated."
                    break # Stop the full script if stopping the AOS is not possible in any way.
                }
            }
        }
    }

    Hopefully, this will be helpful to someone in the future. Sorry if some variable names got mixed up.

    Monday, January 4, 2016 12:43 PM

All replies

  • You need to fix the underlying problem.

    \_(ツ)_/

    Friday, September 4, 2015 7:27 AM
  • Also - Stop-Service waits for the service to stop.  Restart-Service also waits correctly,


    \_(ツ)_/

    Sunday, September 6, 2015 5:50 PM
  • Hello,

    To fix this issue I wrapped the above code in a try-catch block. That way, when the initial error shows, I have control over it.

    So I wrote a function that will wait some more for the service to stop or kill the service if it takes too long. This function is called in the catch block.

    function WaitForAOStoStopOrKill
    {
        $stateAos = gwmi Win32_Service -ComputerName ${env:computername} -Filter "Name LIKE '$($buildAosServiceName)'" | select -expand State
        
        if ($stateAos -eq "Stopping")
        {
            $procssAos = Get-Process -Id (gwmi Win32_Service -ComputerName ${env:computername} -Filter "Name LIKE '$($buildAosServiceName)'" | select -expand ProcessId)
            
            if ($procssAos.WaitForExit(300000) -eq $false) # Wait another 5 minutes for the service to really stop
            {
                Write-Host "Waiting for 5 more minutes for status 'Stopped' on AOS $($ServerInstanceName) succeeded. AOS is now stopped."
            }
            else
            {
                try
                {
                    # Kill the process, based on PID in case multiple AOSes have the same display name
                    Write-Host "Killing the AOS, because it won't stop in the expected way."
                    $pid = gwmi Win32_Service -ComputerName ${env:computername} -Filter "Name LIKE '$($buildAosServiceName)'" | select -expand ProcessId
                    Write-Host "Process ID from service $($buildAosServiceName) on $(${env:computername}) is $($pid)"
                    Write-Host "Process from service $($buildAosServiceName) on $(${env:computername}) killed."
                    Invoke-Command -ComputerName ${env:computername} {Stop-Process $args -Force} -ArgumentList $pid
                }
                catch
                {
                    Write-Host "AOS $($sourceServerInstanceName) couldn't be stopped in any way. Script terminated."
                    break # Stop the full script if stopping the AOS is not possible in any way.
                }
            }
        }
    }

    Hopefully, this will be helpful to someone in the future. Sorry if some variable names got mixed up.

    Monday, January 4, 2016 12:43 PM