PING server2, if responses, reboot then verify PING with server2 again

Hello Experts,
if %errorlevel% == 1 "shutdown \\"

I have a 'Batch file" on Server1 and want to ping Server2 continuously.
a. Provide back to Server1 with the output of the time & date when Server2 is still alive.
b. If server2 is still alive, reboot Server2 via that Batch file's command.
c. Continuing PING, when Server2 is back online, recording the time & date.

I want to verify how long the Servers2 was Online then offline {Down} then it is up {Online} again.

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.

You didn't say what's supposed to happen if the target machine is offline when the script is started; should it just exit, or should it wait for the computer to come online? The following script just exits.
Warning: this script is live, it will reboot the target machine; use a test computer for testing.
@echo off
setlocal enabledelayedexpansion
set Computer=
set Wait=15
echo [%Date%][%Time%] Testing if %Computer% is online.
ping.exe -4 -n 4 %Computer% | find.exe /i "TTL" >NUL
if errorlevel 1 (
	echo %Computer% is offline, can't send reboot signal.
	goto :eof
echo [%Date%][%Time%] Rebooting %Computer% in %Wait% seconds.
echo To cancel, stop the script NOW using ^<Ctrl-C^>.
set /a PingCount = Wait + 1
ping.exe -4 -n %PingCount% localhost >NUL
echo [%Date%][%Time%] Sending reboot signal to %Computer% ...
shutdown.exe /m \\%Computer% /r /f /t 0 /d p:0:0 /c "Shutdown initiated by '%UserDomain%\%UserName%' from '%ComputerName%'."
if errorlevel 1 goto :eof

<NUL set /p Dummy=[%Date%][%Time%] Waiting for %Computer% to go offline .
	<NUL set /p Dummy=.
	ping.exe -4 -n 2 %Computer% | find.exe /i "TTL" >NUL
if not errorlevel 1 goto LoopWaitForOffline
echo [%Date%][%Time%] %Computer% is now offline.

<NUL set /p Dummy=[%Date%][%Time%] Waiting for %Computer% to come online .
	set /p Dummy=.<NUL
	ping.exe -4 -n 2 %Computer% | find.exe /i "TTL" >NUL
if errorlevel 1 goto LoopWaitForOffline
echo [%Date%][%Time%] %Computer% is now online again.

Open in new window

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
Chris MillardCommented:
Based on your question, the initial problem I see is that as soon as Server1 sees that Server2 is online, Server1 will try to shutdown Server2.

What you have is a script that will CONSTANTLY be trying to restart Server2.

The other thing is that upon rebooting, Server2 will respond to pings before it accepts remote commands, so your batch will then fail on trying to reboot the server.

Bear in mind also that network services will shutdown well before the server reboots too, so you aren't going to get any accurate data.
Lonelygirl_2012Author Commented:
Thank you
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
Microsoft DOS

From novice to tech pro — start learning today.