Script to stop services and gracefully reboot the computer

I have a repetitive task that I need to automate with a batch or VBscript.

Sometimes I have a list of Windows computers that I need to RDP to then:
-Stop OpenSsh service
-Stop MySQL Service and the dependent service (I cannot remember its name)
-Confirm that the OpenSsh and MySQL and the other independent service are completely stopped
-Gracefully restart the computer.

I need a script that reads off notepad a list of computers, lined up properly in a column, and   goes to the top list reads the 1st computer name and executes the tasks I mentioned above.
However I need the script to generate another text file as log file, which will contain for instance:
OpenSsh service stopped
MySQL service stopped
XYZ service stopped
Computer1 rebooted

If any of the services cannot be stopped, the script should not reboot the computer, but will log the message in the same text file that this computer has not been rebooted because service XYZ could not be stopped

Any help will be very much appreciated

Thank you
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.

jskfanAuthor Commented:
I cannot understand what is not compliant in the question I posted.
jskfanAuthor Commented:
I did not post any link in my Question
Hazem BadrCommented:
hi jskfan

its not you, its me send the link

thank you
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

The difficulty with this is that a failure to stop a service would likely make things worse.
Let's say your script works to stop Apache, but fails to stop any one of the other services. At this point the server is still running, but the application/functions it performs are degraded.
Trying to introduce logic I.e. Service 1 stopped, service2 stopped, service 3 does not stop, do you forcibly terminate service 3, or do you abandon this process starting service 2, and then service 1?
There is an inherent issue here that service 3 is effectively in an unknown state.  Having received a notice to stop, it might be taking longer to perform the requisite steps to properly exit.  The result could be that shortly after service 2 and service 1 start up again, service 3 completes all the outstanding tasks and exits.  Now you have a critical service down and your restart process attempt is done.

What issues is your system running into that a reboot us required?
And are you ok with server/systems in zombie states?  Some services stopped in a timely manner with one service not stopping.

Do you have intra server dependencies server a service 1, relies on service 3 on server b?
jskfanAuthor Commented:
If any of the services has not stopped then there is no reboot needed.
I guess  the script can pause for a couple of minutes till the services are stopped, if 2 minutes are too short we can increase it, if any of the services does not stop after  a determined time then the script can log a message "unable to stop the service",

Also, I can run separate script for each computer if needs be...

for instance the script will popup a dialog box I will pas computer name there and click, the script will proceed with stopping services and reboot the computer, then run the script again and paste the next computer name in the dialog box,....and so on
jskfanAuthor Commented:
At any rate,  it will still be less tedious than RDP to each computer, stop services and reboot computer
Using a vbscript/powershell as well as Psexec you can do what you are asking for

The services you outline will terminate might take longer.

Certain things can be stopped via/using wmi interface.

If you need the server rebooted, issue the reboot. The system will direct the running services to stop waiting for a. Set amount of time for the process to gracefully exit or it will be terminated and the system rebooted. (/force) should be part of the reboot directive, or you will end up with an u reachable zombie system.
Why do you feel you need to stop the services first?
Each application/service OpenSSL, MySQL as other services have built in process to manage the graceful exit prices when notified to terminate.

Psexec or shutdown can be used
Shutdown /m  <syste name> /r /f /t delay_in_sec_to_start_process
With Psexec /s  <system name> 'Shutdown /r /t delay_in_sec_to_start_process

You can create if not already a GPO that has startup/shutdown scripts that run as the system that will record their bootup/shutdown time events.

Presumably, you have the server/application dependency........

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
jskfanAuthor 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
Windows Batch

From novice to tech pro — start learning today.