Howdy -
Thank you for the response, I’m still working at this. The service I’m checking now is the RPC server and I found a better “batch file script” online than what I had so I am trying that, but it’s still the same process basically and I think it could be better. I’d like the emails to be formatted so it’s the server name - reboot start time - time server came back online, and anything else useful which I could add. I’m still trying to figure out how to do this in powershell and not having luck at all. One issue I have with this script is that I often get the “hang on reboot” alert emails and if I increase the time limit then it takes a long time to reboot the list of servers as some reboot quicker, I know I can’t fix that as it’s based on the server, but maybe a way to not have the hung reboot alerts unless it’s really hung? I can send email from the system that I run this on.
echo off
REM ********************************
REM Set the time limits for the items below
REM Set the max time here for the time for the server to shutdown
set ST=1200
REM Set the max time in SECONDS for a server to complete POST and start booting
set BT=200
REM Set the max time in MINUTES for a server to start after POST
set CT=10
REM Set the name of the mail server:
set MLSVR=mail.testdomain.local
REM Set the TO: email address:
set TOEML=Jake@testdomain.local
REM Set the From: email address:
set FRMEML=jake@testdomain.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% *** > log.txt
echo %USERNAME% on %COMPUTERNAME% has initiated the script at %CD% >> log.txt
REM Checking if the list of servers for reboots is available.
if NOT EXIST computers.txt goto nocomputers
REM Listing the servers that will be rebooted.
echo The following Servers will be rebooted: >> log.txt
echo The following Servers will be rebooted:
type computers.txt >> log.txt
type computers.txt
echo. >> log.txt
echo.
:routinestart
REM Parse file and call the reboot subroutine with the server name as argument.
for /F %%i in (computers.txt) do (call :restartscript %%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 The actual reboot script.
:restartscript
REM Initializing the Log file.
set COMP=%1
echo. >> log.txt
echo.
echo %DATE% %TIME% *** Log for %COMP% >> log.txt
echo %DATE% %TIME% *** Log for %COMP%
echo %DATE% %TIME% Checking if %COMP% is up >> log.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 offline
goto startreboots
REM Rebooting the server.
:startreboots
echo %DATE% %TIME% %COMP% is Up >> log.txt
echo %DATE% %TIME% %COMP% is Up
echo %DATE% %TIME% Rebooting %COMP% >> log.txt
echo %DATE% %TIME% Rebooting %COMP%
shutdown -r -f -m \%COMP% -t 0
echo %DATE% %TIME% %COMP% is rebooting… please wait… >> log.txt
echo %DATE% %TIME% %COMP% is rebooting… 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 computerhung
:down
echo %DATE% %TIME% %COMP% is down, waiting for server to come up >> log.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 computerhung
:start
echo %DATE% %TIME% %COMP% is starting up… please wait for a minute… >> log.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% >> log.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 online
echo %DATE% %TIME% The RPC Service has not started… >> log.txt
echo %DATE% %TIME% The RPC Service has not started…
)
set CAUSE=has hung on Boot
goto computerhung
:online
echo %DATE% %TIME% %COMP% is up - RPC Service is running. >> log.txt
echo %DATE% %TIME% %COMP% is up - RPC Service is running.
goto eof
:computerhung
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
:offline
echo %DATE% %TIME% %COMP% not reachable… skipping >> log.txt
echo %DATE% %TIME% %COMP% not reachable… skipping
goto eof
:nocomputers
echo Make sure the file “%CD%\computers.txt” exists and is readable >> log.txt
echo Make sure the file “%CD%\computers.txt” exists and is readable
echo Stopping Script execution now. >> log.txt
echo Stopping Script execution now.
goto cleanup
:eof