script to automatically force kill windows service with different PID each time.

script to automatically force kill windows service with different PID each time.  

I have a windows service that gets hung after a security scan.  We have a long term fix but I need a short term fix.
The only way to fix is to reboot or force kill the service since it becomes grayed out after one fail and the attempt to restart.  
It seems to run:

sc queryex "AppNAME"

taskkill /F /PID 4500

sc start "APPNAME"

This works while doing manually every time on the server since the PID changes.
How can I write a batch script that will fire off once it cannot restart 2/3 times?
How can the variable of the different PID each time be carried over to the taskkill?
Is there a better way to do this?  PowerShell Script?
This is a Windows Server 2008R2 box and the longterm plan is I'm building a Server 2016 virtual machine to move application.
System AdminAsked:
Who is Participating?
You can "taskkill" by process (image) name:
taskkill.exe /f /im "APPNAME.EXE"
sc.exe start "APPNAME"

Open in new window

Or in plain PowerShell:
Stop-Process -Force -Name APPNAME
Start-Service -Name APPNAME

Open in new window

System AdminAuthor Commented:
@ oBda

This solution once fixed seems like it will work.  I see your point, don't use PID use something else.
When I tried CMD and APPNAME it did stop it but not the service, then the app just restarted and loaded.

How can I changed to service name?

PowerShell did not like anything for the APPNAME

Once I get the code right.  Under services and Recovery I can just add run program and point to .bat or .ps1 correct?
I'm also thinking if that doesn't work to add Restart the box.
Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

System AdminAuthor Commented:
Or a better questions how do I just taskkill with Service name?

Not PID or not IMAGENAME because .exe seems to not be working.

I tried taskkill /F /fi "service eq service name"

It says not task running with the specified criteria but i know it is because i can see the service running with the exact name i used.
System AdminAuthor Commented:
Ok it looks like a simple

Stop-Service ServiceName -force
Start-Service Servicename works

Taskkill just is not friendly with service names and PS seems to be better.  (OF course)
Now my next task it to convert to .ps1 and then add to the recovery tab to run as program?
System AdminAuthor Commented:
Sorry for so many post.  For my .PS1 script should I have any delay or will it give time to force the stop. then start cleanly?  Maybe it knows to do that.  I just went to services, recovery. First failure to run program, all files, then pointed to my service restart.ps1 script.  

Stop-Service ServiceName -force
Start-Service Servicename
In the taskkill/Stop-Process, APPNAME needs to the name of the service executable (from the properties of the service in the MMC).
taskkill wants it with the extension, PowerShell wants just the file name.
If Stop-Service works for you when the service crashed, it's best to use this; somewhat surprising if the MMC doesn't allow you to control the service anymore.
Anyway, if Stop-Service works for you, you can then even try just a simple
Restart-Service -Force -Name ServiceName
For something as simple as that, you don't even need a script, you can just do it with a single command line (note the use of single and double quotes!):
powershell.exe -command "Stop-Service 'ServiceName'; Start-Service 'ServiceName'"

Open in new window

System AdminAuthor Commented:
Just to add the way the service fails it the service goes into panic or failure.  The service stop,start,restart become Grayed out in windows.

Hence the need for a force kill
The recovery mode is not working because all it tries to do is restart and if the service is hung it cannot do anything.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.