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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.