Hello - I have read several posts online about how to do this task and I am not finding one that works really.
The ask of me is to restart servers from a list inside a text file and reboot them one at a time, then send an email if the system hangs on reboot and then the results when the systems reboot. The batch script that I've pasted below does this, but it's not as stable I think PowerShell would be and also I want to add to this and I know I'll be limited when I try to do that.
- REM This script will reboot the list of servers specified in the serverlist.txt file
- REM List the servers you want to reboot in order from first to last in serverlist.txt
- REM NOTE: Hostnames are only allowed, this does not work with IP Addresses
- REM Put the list of servers in the same folder as this script
- REM The log will go into the same folder as this script and server list
- REM This script is using a free mail tool - bmail - that has to be in the folder as well
- REM ********************************
- REM Parameters that you can change to whatever you like
- REM Set the max time in SECONDS for a server to shutdown
- set ST=1200
- REM Set the max time in SECONDS for a server to complete POST process and start bootup
- set BT=400
- REM Set the max time in MINUTES for a server to start after POST process
- set CT=10
- REM Set the name of the mail server:
- set MLSVR=smtp.test.local
- REM Set the TO: email address:
- set TOEML=Admin@test.local
- REM Set the From: email address:
- set FRMEML=AutoReboot@test.local
- REM End of Tunable Parameters
- REM ********************************
- REM Finding the Local Timezone
- for /f "tokens=1,2*" %%K in ( ' reg query HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\TimeZoneInformation ' ) do if %%K==StandardName set timezone=%%M
- REM Creating the temporary Log file
- echo ***Logging Started on %DATE% %TIME% %TIMEZONE% *** > rebootlog.txt
- echo %USERNAME% on %COMPUTERNAME% has initiated the script at %CD% >> rebootlog.txt
- REM Checking if the list of servers for reboots is available.
- if NOT EXIST serverlist.txt goto noserverlist
- REM Listing the servers that will be rebooted.
- echo The following Servers will be rebooted: >> rebootlog.txt
- echo The following Servers will be rebooted:
- type serverlist.txt >> rebootlog.txt
- type serverlist.txt
- echo. >> rebootlog.txt
- echo.
- REM NOTE: This is an option - if you want to add a confirmation prompt then uncomment out the lines below
- :choice
- REM set /P c=Are you sure you want to continue[Y/N]?
- REM if /I "%c%" EQU "Y" echo %USERNAME% has accepted the warning at %DATE% %TIME% and proceeded. >> rebootlog.txt && goto :routinestart
- REM if /I "%c%" EQU "N" echo %USERNAME% has chosen to exit at %DATE% %TIME% >> rebootlog.txt && goto :cleanup
- REM goto :choice
- :routinestart
- REM Parse file and call the reboot subroutine with the server name as argument.
- for /F %%i in (serverlist.txt) do (call :rebootscript %%i)
- REM Subroutine to rename the temporary log file and then email the final log.
- :cleanup
- for /f "tokens=1-5 delims=/ " %%d in ("%date%") do (
- echo. >>%USERDOMAIN%-%%g-%%f-%%e.txt
- type rebootlog.txt >> %USERDOMAIN%-%%g-%%f-%%e.txt
- del /F rebootlog.txt
- bmail -s %MLSVR% -t %TOEML% -f %FRMEML% -h -a "%USERDOMAIN% Server Reboot Log" -m %USERDOMAIN%-%%g-%%f-%%e.txt
- )
- goto eof
- REM This is the part of the script that does the reboots
- :rebootscript
- REM Initializing the Log file.
- set COMP=%1
- echo. >> rebootlog.txt
- echo.
- echo %DATE% %TIME% *** Log for %COMP% >> rebootlog.txt
- echo %DATE% %TIME% *** Log for %COMP%
- echo %DATE% %TIME% Checking if %COMP% is up >> rebootlog.txt
- echo %DATE% %TIME% Checking if %COMP% is up
- REM Checking if server is up if it is not up, skip this server and go to the next one.
- ping %COMP% > nul
- if %ERRORLEVEL% NEQ 0 goto notreachable
- goto rebootstart
- REM Rebooting the server.
- :rebootstart
- echo %DATE% %TIME% %COMP% is Up >> rebootlog.txt
- echo %DATE% %TIME% %COMP% is Up
- echo %DATE% %TIME% Rebooting %COMP% >> rebootlog.txt
- echo %DATE% %TIME% Rebooting %COMP%
- shutdown -r -f -m \\%COMP% -t 0
- echo %DATE% %TIME% %COMP% is getting rebooted... please wait... >> rebootlog.txt
- echo %DATE% %TIME% %COMP% is getting rebooted... please wait...
- for /L %%a in (1,1,%ST%) do (
- ping -n 2 -w 1 %COMP% > nul
- if ERRORLEVEL 1 goto down
- )
- set CAUSE=has hung on shutdown
- goto serverhung
- :down
- echo %DATE% %TIME% %COMP% is down, waiting for server to come up >> rebootlog.txt
- echo %DATE% %TIME% %COMP% is down, waiting for server to come up
- for /L %%a in (1,1,%BT%) do (
- ping -n 2 -w 1 %COMP% > nul
- if NOT ERRORLEVEL 1 goto start
- )
- set CAUSE=has hung during or after POST.
- goto serverhung
- :start
- echo %DATE% %TIME% %COMP% is starting up... please wait for a minute... >> rebootlog.txt
- echo %DATE% %TIME% %COMP% is starting up... please wait for a minute...
- for /L %%a in (1,1,%CT%) do (
- ping -n 40 -w 1 %COMP% > nul
- echo %DATE% %TIME% Checking the RPC Service status... Pass %%a of %CT% >> rebootlog.txt
- echo %DATE% %TIME% Checking the RPC Service status... Pass %%a of %CT%
- sc "\\%COMP%" query RpcSs | FIND "RUNNING" > nul
- if NOT ERRORLEVEL 1 goto serverup
- echo %DATE% %TIME% The RPC Service has not started... >> rebootlog.txt
- echo %DATE% %TIME% The RPC Service has not started...
- )
- set CAUSE=has hung on Boot
- goto serverhung
- :serverup
- echo %DATE% %TIME% %COMP% is up - RPC Service is running. >> rebootlog.txt
- echo %DATE% %TIME% %COMP% is up - RPC Service is running.
- goto eof
- :serverhung
- bmail -s %MLSVR% -t %TOEML% -f %FRMEML% -b "%USERDOMAIN% - %DATE% %TIME% %TIMEZONE% %COMP% %CAUSE%" -h -a "%USERDOMAIN% - %COMP% %CAUSE%"
- echo %DATE% %TIME% %COMP% %CAUSE%. Skipping to next server. >> rebootlog.txt
- echo %DATE% %TIME% %COMP% %CAUSE%. Skipping to next server.
- goto eof
- :notreachable
- echo %DATE% %TIME% %COMP% not reachable... skipping >> rebootlog.txt
- echo %DATE% %TIME% %COMP% not reachable... skipping
- goto eof
- :noserverlist
- echo Make sure the file "%CD%\serverlist.txt" exists and is readable >> rebootlog.txt
- echo Make sure the file "%CD%\serverlist.txt" exists and is readable
- echo Stopping Script execution now. >> rebootlog.txt
- echo Stopping Script execution now.
- goto cleanup
- :eof