Batch Scripting - Wait until network Established

14,032

Solution 1

Simple check. Doesn't exit after automatically after a duration. Worked for what I needed.

:CHECKFOLDER
TIMEOUT 1
IF NOT EXIST C:\TEST GOTO CHECKFOLDER

Solution 2

My answers on How to ping a server only once from within a batch file? and Deleting Desktop Shortcuts Associated With Network Drives? contain useful information for this task.

Here is a template batch script for 1 server:

@echo off
set "Server=192.168.0.2"
set "RetryCount=0"

:CheckServer
rem Ping server only once with a time limit of 1000 ms.
%SystemRoot%\System32\ping.exe -n 1 -w 1000 %Server% >nul
if not errorlevel 1 goto MapDrive

rem Connection to server is not yet established. Give up after 30 seconds
rem in case of not being in right LAN or no network connection at all or
rem this server is currently not running.
set /A RetryCount+=1
if not %RetryCount%==30 goto CheckServer

echo There is no connection to server %Server%.
echo The network drive X: is not available.
goto :EOF

:MapDrive
echo Mapping network drive X: ...
%SystemRoot%\System32\net.exe use X: \\%Server%\share1 /persistent:no >nul

I must add that this solution does not work if on the servers a firewall is running which blocks pings to server and therefore the workstations do not get ever an answer on their server pings.

Solution 3

You should use Group Policy Preferences to map network drives, not batch scripts, however, if you insist, pinging the server in the script is not the right way to do this.

Instead you should deploy the group policy setting:

"Computer Configuration" -> "Administrative Templates" -> "System" -> "Logon" -> "Always wait for the network at computer startup and logon"

This will force the computer to wait for the network to come up before running startup/login scripts.

Share:
14,032
Lonnie Best
Author by

Lonnie Best

Do your work willingly, as though you were serving the Lord himself, and not just your earthly master. -Colossians 3:23 The thing about quotes on the internet is that you cannot confirm their validity. -Abraham Lincoln

Updated on July 10, 2022

Comments

  • Lonnie Best
    Lonnie Best almost 2 years

    I have logon.bat script that runs each time a user logs into their workstation of an Active Directory domain.

    This script maps some network drives with commands like this:

    net use x: \\192.168.0.2\share1
    net use y: \\192.168.0.3\share2
    net use z: \\192.168.0.4\share3
    

    However, for some workstations, this script is running before the the workstation itself has fully established its own LAN connection. The user then goes to Start Menu > Computer, and see red X's for each mapped drive. By this time, the network is established, so if they click on the red X, it goes away. Still, I would like the logon.bat script to verify the network is established before running the net-use commands.

    I've seen solutions for this that require settings to be changed on group policy or require changes to settings on the culprit-workstation. However, I'm interested in a solution that solves this exclusively within my current batch script (without requiring any additional script-dependencies outside of that script).

    I've seen other scripting solutions that simply put a timeout (or sleep) into the batch script so that it waits an arbitrary amount of time before running the net use commands. These solutions provide wait-constants that either "map the drives a little too much later than they could've been mapped" or "still risk running the net-use commands earlier than network is established".

    Instead, I'd like a script that would check every second to see if the network has been established, and then (upon success) proceed to run the "net use" commands mentioned previously.

    How do you achieve this in a Windows batch script?