If you are like me and have multiple Windows DNS servers to manage, you find that it gets tiresome to constantly update, clear the cache and restart the DNS Server service. Scripting to the Rescue!
Here is a batch file I use to automate the process, it includes a method of writing a log file witch you can turn off or locate anywhere and allows for any number of server to be defined, thanks to a method of using the “FOR” command that reads and array of any length. Special thanks to all of those DOS Gurus that take the time to post their code and share their experiences!
::RESTART_DNS ::Written by Frank J. McCourry ::4/2016 ECHO OFF :: Set all variables Local SETLOCAL ::Set USELOG to "Yes" to create a log and "No" to suppress all output. SET USELOG=Yes ::Set LogFile Location. Create the folder and file if necessary. :SetLogFilePath IF %USELOG%==No SET LogFile=null If %USELOG%==No Goto SetServers SET LogFileDrive=C: SET LogFilePath=\LOGS\ SET LogFileName=DNS_Restart.log.txt If NOT EXIST %LogFileDrive%%LogFilePath% ( CD %LogFileDrive% MKDIR %LogFilePath% ) :SetLogfile SET LogFile=%LogFileDrive%%LogFilePath%%LogFileName% :SetServers ::Set Variables for Servers by Name or IP address. You can use any number of variables here, just keep them sequential. SET Svr[1]=Server1 SET Svr[2]=Server2 SET Svr[3]=10.0.0.10 SET Svr[4]=10.0.0.11 ::=======================THERE IS NO NEED TO CHANGE ANYTHING BELOW======================== ::Clear the Screen, it just looks nice. CLS ECHO. ::Create Log File if needed ECHO Creating Log %LogFile% ECHO %Date% %Time% Logfile Created 2>>%LogFile% ECHO. ::Process commands to clear the DNS cache, update the server data files and restart the DNS service on each server FOR /F "tokens=2 delims==" %%A IN ('SET Svr[') DO ( ECHO Processing DNS Commands for %%A 2>>%LogFile% ECHO. :ClearCacheAndUpdate ECHO Clearing Cache and updating server files on %%A 2>>%LogFile% ECHO. DNSCMD %%A /ClearCache >>%LogFile% DNSCMD %%A /writebackfiles >>%LogFile% :StopDNS sc \\%%A stop dns >>%LogFile% ECHO Stopping DNS Service on %%A If ERRORLEVEL 1 Goto StopDNS Call :TimeOut ECHO. ECHO %%A DNS Service Status 2>>%LogFile% ECHO ====================================== sc \\%%A query dns | findstr /i "STATE" 2>>%LogFile% ECHO. :StartDNS ECHO Starting DNS Service on %%A sc \\%%A start dns >>%LogFile% If ERRORLEVEL 1 Goto StartDNS Call :TimeOut ECHO. ECHO %%A DNS Service Status 2>>%LogFile% ECHO ====================================== sc \\%%A query dns | findstr /i "STATE" 2>>%LogFile% ECHO. ) :FlushDNSCache ECHO. ipconfig /flushdns >>%LogFile% ECHO DNS Flush on the local machine is complete! 2>>%LogFile% ECHO. ECHO Processing Complete! 2>>%LogFile% ::Cleanup ENDLOCAL :END exit /b :TimeOut Timeout /t 10 /nobreak CLS GOTO :EOF