We help IT Professionals succeed at work.

Script to remotely reboot a series of servers

Hi,

I have a farm with 10 windows servers. I reboot them once a month manually.
I would like to run a batch that will send a few remote commands including a reboot command, wait for the server to return (ping until there is a reply) and then continue to the next server.
Can anyone point me to something I can base my script on?

Thanks in advance
Comment
Watch Question

Author

Commented:
Thanks! that looks good. do you have any suggestion on how would I go about creating an IF that will only continue after the rebooted server responds to ping?
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
Save the script below as FarmReboot.cmd or Whatever.cmd; enter the server names below the section "[DATA]".
The script is currently in test mode and will only display the shutdown commands it would normally run.
Remove the capitalized ECHO in front of shutdown.exe to run it for real.

@echo off
setlocal enabledelayedexpansion
set ScriptName=%~nx0
for /f "tokens=1 delims=[]" %%a in ('type "%~f0" ^| find /n "[DATA]"') do set DataStart=%%a
for /f "tokens=1 skip=%DataStart%" %%a in ('type "%~f0"') do (call :Reboot %%a)
goto :eof
:Reboot
set Server=%~1
echo [%Time%] Rebooting %Server% ...
ECHO shutdown.exe /m \\%Server% /r /c "Reboot initiated by %ScriptName% from %ComputerName%" /t 10
for /f "delims=" %%a in ("%Time%") do set WaitingSince=%%a
:LoopPing
echo [%Time%] Waiting for response from %Server% since %WaitingSince% ...
ping -n 1 -w 1000 %Server% | find /i "TTL" >NUL
if errorlevel 1 goto :LoopPing
goto :eof

[DATA]
Server1
Server2
Server3

Open in new window

Commented:
Anyway you have to monitor the first server until the ping after restart. so better you can manually do the restart for 10 servers, if you require one by one reboot.

You must try below tool, may helpful...

http://www.intelliadmin.com/index.php/2006/05/reboot-computers-on-your-network/

AtB
Kamaraj SubramanianApplication Support Analyst

Commented:
http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_23263343.html?sfQueryTermInfo=1+10+30+reboot+script+server+window

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/Q_23923100.html?sfQueryTermInfo=1+10+30+reboot+script+server+window

Use psshutdown. www.sysinternals.com

Type the command for a complete list of parameters. You can also set the shutdown reason code.

I think the command should look like this as a minimum.

psshutdown -f -r -t 0 @RemoteServers.txt

Commented:
Let me sugest you other VERY DIFFRENT APPROACH:
do not reboot your servers once a month! :D

Serious: Install a monitoring solution (my option is  ZABBIX )  and discover if you need to restart them. Even that, Zabbix can issue the command to restart.
Or you can fix any problem and restart they using Zabbix remote comand capability.

Author

Commented:
My application is EOL and just needs to WORK. I do not invest any more investigation and development on it, so rebooting is the only right solution.

Commented:
Ok. Sorry... I was not aware your app was EOL.
So I suggest you to use itkamaraj tip about remote shutdown.

Author

Commented:
Thanks!