Scripting HELP

I require some assistance with the following:

I am working on a script that detects if the CPU usage is high on the server, then stop/starts the specific offending service (attempts to at least)
Can you advise what I need to add to the script in order to restart the server if a particular (or offending service) doesn't start correctly?

This is what I've got thus far

@Echo OFF
REM Craig Paulsen - Restart WDS Service Beta 1.1


For /F %%P in ('wmic cpu get loadpercentage ^| FINDSTR "[0-9]"') do (
        Echo [%TIME:~0,8%] CPU Usage: %%P%% Reached the limit: %MAXUSAGE%%%
        Echo Stopping %SERVICE% ...
         SC STOP  "%SERVICE%" 1>NUL
      timeout /t 1500
        SC START "%SERVICE%" 1>NUL
        Echo Attempt %SERVICE% restart.
    ) ELSE (
         Echo [%TIME:~0,8%] CPU Usage: %%P%%
Ping -n %INTERVAL% Localhost >NUL

Your help will be most appreciated.

Craig PaulsenSenior Systems EngineerAsked:
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.

Lee W, MVPTechnology and Business Process AdvisorCommented:
The restart command is simple - shutdown -r - f (-r = reboot, -f = force)
Craig PaulsenSenior Systems EngineerAuthor Commented:
I agree Lee, it's simple enough, however as per my question

""what do I need to add to the script in order to restart the server if a particular (or offending service) doesn't start correctly?""

So im wanting it to attempt to restart a particular service, from our monitoring, we know this service "sometimes" doesn't start gracefully, and the only way to restore it is to restart the server, im trying to get that SMARTS built into the above mentioned script
You can use perfmon.

Setup perfmon performance monitoring. It let's you configure actions (scripts', whatever you like) and send alerts. If I need to elaborate on that, please make sure to look at perfmon first.
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

Craig PaulsenSenior Systems EngineerAuthor Commented:
thanks McKnife, can you expand on that please, I've had a look at PerfMon but can't quite work out how I will achieve what I'm wanting....
This link offers a good tutorial: - you can do analogue steps for your purpose.

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
Craig PaulsenSenior Systems EngineerAuthor Commented:
thanks McKnike,
Craig PaulsenSenior Systems EngineerAuthor Commented:
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
VB Script

From novice to tech pro — start learning today.