@echo off CLS rem On the 2 servers rem Run this on the source server beforehands, then AddNode.bat on the destination rem Install support tools available on Windows CD to get the dnscmd command line rem Edit environnement variable PATH to run the command SETLOCAL ENABLEDELAYEDEXPANSION rem Variables to edit set DSTSRV=server2 set SRCSRV=server1 set DNSSRV=mydnsserver rem File server DNS alias (without domain), usually one set CNAMES=DNS1,DNS2 rem Volumes on shared storage to migrate set DRIVES=X,Y set KEYPATH=HKLM\SYSTEM\CurrentControlSet\Services\lanmanserver\Shares set SHARESRC=%KEYPATH% set SHAREDST=\\%DSTSRV%\%KEYPATH% set SECURITYSRC=%KEYPATH%\Security set SECURITYDST=\\%DSTSRV%\%KEYPATH%\Security set diskfile=disk.txt rem Check drives are available on target for /D %%D in (%DRIVES%) DO ( reg query \\%DSTSRV%\HKLM\SYSTEM\MountedDevices /v \DosDevices\%%D: 2>NUL 1>NUL if not !ERRORLEVEL!==1 ( echo %%D is not available on target echo CANCELLING FAILOVER goto end ) ) echo Drive letters available on %DSTSRV% echo =^> STARTING FAILOVER echo. rem ****************************************************************************************** rem This part only on Main server rem The main server is the share names reference rem Copy share names on target rem Delete previous share names on target for /D %%D in (%DRIVES%) DO ( rem Go though shares for /F "tokens=1 delims=" %%K in ('reg query %SHAREDST%^|findstr %%D:') do ( rem reg query outputs 3 fields: Value - Type - Data rem We swap the type for # since only single characters can delimit rem All this needed for share names with white spaces! set SHAREKEY=%%K set SHAREKEY=!SHAREKEY:REG_MULTI_SZ=#! rem Retrieve value (sharename) and data (path) for /F "tokens=1,2 delims=#" %%S in ("!SHAREKEY!") do ( rem Remove leading and trailing whitespace rem and adds double quotes for share names containing spaces set SHARE=%%S set SHARE="!SHARE:~4,-4!" rem Remove share names and security reg delete %SHAREDST% /v !SHARE! /f reg delete %SECURITYDST% /v !SHARE! /f ) ) ) rem Copy shares name and security on to the backup server registry rem Go though drives for /D %%D in (%DRIVES%) DO ( rem Go through shares for /F "tokens=1 delims=" %%K in ('reg query %SHARESRC%^|findstr %%D:') do ( rem reg query outputs 3 fields: Value - Type - Data rem We swap the type for # since only single characters can delimit rem All this needed for share names with white spaces! set SHAREKEY=%%K set SHAREKEY=!SHAREKEY:REG_MULTI_SZ=#! rem Retrieve value (sharename) and data (path) for /F "tokens=1,2 delims=#" %%S in ("!SHAREKEY!") do ( rem Remove leading and trailing whitespace rem and adds double quotes for share names containing spaces set SHARE=%%S set SHARE="!SHARE:~4,-4!" set DATA=%%T set DATA="!DATA:~4!" rem Restore shares name reg add %SHAREDST% /v !SHARE! /t REG_MULTI_SZ /d !DATA! /f rem Same method as above for security for /F "tokens=1 delims=" %%L in ('reg query %SECURITYSRC% /v !SHARE! ') do ( set SECURITYKEY=%%L set SECURITYKEY=!SECURITYKEY:REG_BINARY =#! for /F "tokens=2 delims=#" %%S in ("!SECURITYKEY!") do ( REG ADD %SECURITYDST% /v !SHARE! /t REG_BINARY /d %%S /f ) ) ) ) ) rem Share names have been copied on to the destination server rem section should also be scheduled daily to replicate the shares rem in case the main sever goes down: all shares would be lost rem ****************************************************************************************** rem rem Failover is starting here rem rem Extra, exports and stops DHCP config echo Exporting and stopping DHCP netsh dhcp server export "\\%DSTSRV%\C$\Scripts\DHCP-Backup.txt" all net stop DHCPServer echo. rem Stop services beforehands to remove drive letters echo Stopping services net session /delete /y net stop "Server" /y echo. rem Remove drive letters echo Removing drives letters copy NUL %diskfile% for /D %%D in (%DRIVES%) DO ( echo select volume %%D: >> %diskfile% echo remove >> %diskfile% ) diskpart /s %diskfile% echo. rem Restart services rem "Server" service is started with others since they depend on it echo Restarting services net start "Net Logon" net start "Computer Browser" net start "Distributed File System" rem Reassign identical drive letters on target server rem And Restart service "Server" :end ENDLOCAL