Answered by:
Restart Service

Question
-
Would like to re-start a service on schedule.
I'm able to re-start service but half the time it is hung on Stopping.
When it is hung, I have to stop the service by ending the task or using stop-process -name ServiceName.
Help with script:
Restart-service -name 'servicename'
wait 60 seconds
If hung kill process using servicename
start service
Thanks!
Tuesday, September 26, 2017 7:30 PM
Answers
-
You need to fix the service
\_(ツ)_/
- Proposed as answer by Tim Buntrock Monday, October 9, 2017 2:34 PM
- Marked as answer by Andy Diaz Tuesday, October 10, 2017 6:12 PM
Saturday, October 7, 2017 5:11 PM
All replies
-
Stop-Service servicename -Force
or
if(ReStart-Service servicename -PassThru -Force -ea 0){'success'}else{'failed'}
\_(ツ)_/
Tuesday, September 26, 2017 7:52 PM -
Tried the -Force and does not work.
Will try 2nd option in a few days. Service hangs after a few days.
Thanks!
Tuesday, September 26, 2017 8:27 PM -
Hi Andy,
Basically you want to set a timeout for restart-service cmdlet, without reinvent the wheel I suggest to use a background job as shown here : https://stackoverflow.com/questions/8566824/powershell-timeout-service
But it's a workaround more than a solution in this case, I think is worth the effort to investigate why the service hangs at "Stopping" status.
Regards
Wednesday, September 27, 2017 2:31 AM -
Unfortunately I did spend time with support on the issue, re-installing, changing settings, going thru logs and debugging but nothing worked. I pretty much gave up with support.
Thanks!
Wednesday, September 27, 2017 2:52 AM -
Hi,
Based on my research, you could have a try with the following scripts, please try Stop-Service -Name $servicename -Force first, if it doesn’t work, please try taskkill /f /pid $processid. Hope it is helpful to you:
$servicename = 'servicename' Restart-Service -Name $servicename Start-Sleep -Seconds 60 #Wait 60 seconds $status = (Get-Service -Name $servicename).Status $processid = (Get-WmiObject -Class Win32_Service | ?{$_.Name -eq $servicename}).ProcessID if ($status -ne 'Running') { # Stop-Service -Name $servicename -Force # taskkill /f /pid $processid Start-Service -Name $servicename -PassThru }
If you need further help, please feel free to let us know.
Best Regards,
Albert LingPlease remember to mark the replies as an answers if they help and unmark them if they provide no help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.- Proposed as answer by Albert LingMicrosoft contingent staff Friday, September 29, 2017 9:47 AM
Wednesday, September 27, 2017 5:55 AM -
Hi,
Just checking in to see if the information provided was helpful. Does the scripts work?
Please let us know if you would like further assistance.
Best Regards,
Albert LingPlease remember to mark the replies as an answers if they help and unmark them if they provide no help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.Monday, October 2, 2017 7:50 AM -
My bad, I ran the script within the ISE window, rather than just running the script.
From ISE it just keeps trying to stop the service and won't time out.
Will try again in a few days.
Thanks!
Monday, October 2, 2017 4:32 PM -
Hello Andy,
There may be various reasons that service hung in stopping. It also depends on what service are you trying to stop. And what are its dependencies services. Please let us know the service that you're trying to stop. It help us to guide you better.
---
Branav.Tuesday, October 3, 2017 2:49 PM -
No dependencies. It is an email archiving scheduler service.Tuesday, October 3, 2017 4:07 PM
-
Hi,
I am checking how the issue is going, if you still have any questions, please feel free to contact us.
If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
If no, please reply and tell us the current situation in order to provide further help.
Appreciate for your feedback.
Best Regards,
Albert LingPlease remember to mark the replies as an answers if they help and unmark them if they provide no help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.Thursday, October 5, 2017 8:38 AM -
Hi Albert,
The script did not work. Stuck on trying to stop the service. Looks like PShell will not process next command, stuck on re-start command.
Saturday, October 7, 2017 5:02 PM -
You need to fix the service
\_(ツ)_/
- Proposed as answer by Tim Buntrock Monday, October 9, 2017 2:34 PM
- Marked as answer by Andy Diaz Tuesday, October 10, 2017 6:12 PM
Saturday, October 7, 2017 5:11 PM