How to wait for network in a batch script when booting on Windows Server?

Solution 1:

You seem to be intent on using batch for this so here goes. You may want to run this by the guys at SO since they are better scripters than I am. In fact, I got the main logic from (https://stackoverflow.com/questions/21245545/ping-test-using-bat-file-trouble-with-errorlevel) complete with the reason why testing if a ping will go through with this method is prefereable to others.

@echo off

set IPaddress=%%1  REM add IP address as command line argument or just static

:TEST
ping -n 1 %IPaddress% | find "TTL=" >nul
if errorlevel 1 (
    goto RETRY
) else (
    goto DOSTUFF
)

:RETRY
ping 127.0.0.1 -n 11>nul REM waits given amount of time, set to 10 seconds
goto TEST

:DOSTUFF
do stuff REM Do stuff
exit

Basically, it just pings at one second intervals, checks to see if a successful ping occurs. If so, it does whatever you're trying to script.

I did not hard code the IPaddress you want to check for basic ICMP connectivity for, but instead added it as a command line argument. Or you can just hard-code it.

You may want to double check the syntax as I did not test this at all.

EDIT: Changed ping value to 11 to reflect a real 10 second delay.

Solution 2:

Use the "Start only if this network connection is available" option. enter image description here