PowerShell stop windows service

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.

# Get the System.ServiceProcess.ServiceController object and the WMI WIN32_Service object.

$targetAosServiceObject=(get-service-ComputerName${env:computername}-Name$targetAosServiceName)

$targetAosServiceWmiObject=(Get-WmiObject-computer${env:computername}Win32_Service-Filter"Name='$targetAosServiceName'")

if($targetAosServiceObject.status -eq'Running') # if the AOS is running

{

Write-Host"Stopping AOS $($targetServerInstanceName)."

$targetAosServiceObject|set-service-StartupType'Disabled'# Disable the service to prevent automatic restart.

$targetAosServiceObject.Stop() |out-null# Stop the service, but return immediately.

$targetAosServiceObject.WaitForStatus('Stopped') # Wait for the service to completely stop.

$targetAosServiceObject.Refresh() # Refresh the current status before checking the status condition.

Write-Host"AOS $($targetServerInstanceName)stopped."

}

else

{

Write-Host"AOS $($targetServerInstanceName)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

September 4th, 2015 2:57am

You need to fix the underlying problem.
Free Windows Admin Tool Kit Click here and download it now
September 4th, 2015 3:34am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics