::http://blogs.technet.com/b/teamdhcp/archive/2009/02/18/migration-of-dhcp-server-from-windows-server-2003-to-windows-server-2008.aspx @REM DhcpMig. @ECHO OFF @REM @REM Variable decleration. SET OPERATION= SET DEST=DHCP_MIG SET DHCP_EXPORT_FILE=dhcp_export.exp SET REGISTRY_EXPORT_FILE=dhcp_reg_export.reg SET DATABASE_FILE_PATH= SET LOG_FILE_PATH= SET LOGV6_FILE_PATH= SET FORCE=0 SET TEMP= SET DEST_SPECIFIED=0 SET Choice=N SET del_choice=N @REM @REM Command line arguments validation. IF (%1)==() ( GOTO HELP ) ELSE ( SET OPERATION=%1 ) IF NOT (%2)==() IF (%2)==(-f) ( SET FORCE=1 ) IF NOT (%2)==() IF NOT (%2)==(-f) ( SET DEST=%2 SET DEST_SPECIFIED=1 ) IF NOT (%3)==() IF NOT (%2)==(-f) ( echo. echo Wrong Arguments: Please refer the help below GOTO HELP ) IF NOT (%3)==() ( SET DEST=%3 SET DEST_SPECIFIED=1 ) echo. @REM @REM Call Subroutine based on operation specified. IF /I %OPERATION% == -export ( GOTO EXPORT ) ELSE IF /I %OPERATION% == -import ( GOTO IMPORT ) ELSE IF /I %OPERATION% == -prereq ( GOTO Prereq ) ELSE ( GOTO HELP ) @REM ---------------------------------------------------------------------- @REM Export Subroutine. @REM :EXPORT @REM Validate if server is installed. sc queryex dhcpserver | findstr /i /c:"EnumQueryServicesStatus:OpenService FAILED 1060" >nul 2>nul if %ERRORLEVEL% == 0 ( echo ERROR:Dhcp server does not seen to have been installed on this machine. echo Please run the script on a correct server. GOTO:EOF ) @REM Validate if server is running. Start if -f is specified else bail-out. sc queryex dhcpserver | findstr STATE | findstr RUNNING 1>nul 2>nul if not %ERRORLEVEL% == 0 ( IF %FORCE% == 0 ( echo Dhcp Server is not running on the Local machine. echo Specify -f option to start the Server and perform an export. GOTO:EOF ) IF %FORCE% == 1 ( net start dhcpserver ) ) @REM Set Log file paths by reading the registry. SET LOG_FILE_REG= reg query HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\DHCPServer\Parameters /v DhcpLogFilePath FOR /F "usebackq delims==" %%z in (`%LOG_FILE_REG%`) do SET TEMP=%%z SET LOG_FILE_REG=%TEMP% for %%z in (%LOG_FILE_REG%) do SET LOG_FILE_PATH=%%z SET LOG_FILE_REG= reg query HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\DHCPServer\Parameters /v DhcpV6LogFilePath FOR /F "usebackq delims==" %%z in (`%LOG_FILE_REG%`) do SET TEMP=%%z SET LOG_FILE_REG=%TEMP% for %%z in (%LOG_FILE_REG%) do SET LOGV6_FILE_PATH=%%z @REM Create the destination directory if it does not exist. IF %DEST_SPECIFIED% == 0 ( echo No destination directory specified. echo Settings will be exported to %DEST% in the Local directory. ) if not exist %DEST%\* ( md %DEST% IF not errorlevel 0 ( echo Cannot create "%DEST%" directory. Please check permissions. ) ELSE ( echo Successfully created "%DEST%" directory. ) ) @REM Validate if the destination directory is empty. @REM If -f is specified, delete the contents else bail-out. if exist %DEST%\dhcp*.* ( echo Directory %DEST% is not empty. IF %FORCE% == 0 ( echo Backup the files and start again or echo Please specify a different directory or echo Specify '-f' option to overwrite directory contents. GOTO:EOF ) IF %FORCE% == 1 ( echo Force deleting the contents of directory %DEST% pushd %DEST% del /q *.* 1>nul 2>nul if exist Logs ( pushd Logs del /q *.* 1>nul 2>nul popd rmdir Logs ) popd ) ) @REM Invoke Export command. pushd %DEST% echo Exporting Server configurations to the file %DHCP_EXPORT_FILE% netsh dhcp server export %DHCP_EXPORT_FILE% all 1>nul 2>nul @REM Export registry settings. echo Exporting registry settings to %REGISTRY_EXPORT_FILE% REG EXPORT HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\DHCPServer\Parameters %REGISTRY_EXPORT_FILE% @REM Remove all "Paths" from the registry export.. (Paths will not be migrated) type %REGISTRY_EXPORT_FILE% | findstr /vi path > temp.reg del %REGISTRY_EXPORT_FILE% ren temp.reg %REGISTRY_EXPORT_FILE% @REM Copy the log files from the location specified in the registry. md Logs if %LOG_FILE_PATH%==%LOGV6_FILE_PATH% ( echo Backing up Log Files from %LOG_FILE_PATH% copy %LOG_FILE_PATH%\Dhcp*.log logs 1>nul 2>nul ) ELSE ( echo Backing up DHCP Log Files from %LOG_FILE_PATH% copy %LOG_FILE_PATH%\Dhcp*.log logs 1>nul 2>nul echo Backing up DHCPV6 Log Files from %LOGV6_FILE_PATH% copy %LOGV6_FILE_PATH%\Dhcp*.log logs 1>nul 2>nul ) echo. echo Settings exported successfully. popd GOTO:EOF @REM ---------------------------------------------------------------------- @REM Import Subroutine. @REM :IMPORT @REM Validate source directory and bail-out if it does not exist. if not exist %DEST%\* ( echo The directory %DEST% does not exist or is not accessible. echo. echo Please provide a correct directory and start again. popd GOTO:EOF ) @REM Validate individual files and bail-out if they do not exist. pushd %DEST% if not exist %DHCP_EXPORT_FILE% ( echo %DEST%\%DHCP_EXPORT_FILE% file does not exist or is not accessible. echo. echo Please provide a correct directory and start again. popd GOTO:EOF ) if not exist %REGISTRY_EXPORT_FILE% ( echo %DEST%\%REGISTRY_EXPORT_FILE% file does not exist or is not accessible. echo. echo Please provide a correct directory and start again. popd GOTO:EOF ) if not exist Logs\* ( echo Directory %DEST% does not contain the "Logs" directory. echo. echo Please verify and provide a correct directory and start again. popd GOTO:EOF ) @REM Check if DHCP Server is installed. Bail-out if not. sc queryex dhcpserver | findstr /i /c:"EnumQueryServicesStatus:OpenService FAILED 1060" >nul 2>nul if not %ERRORLEVEL% == 0 goto DhcpServerInstalled echo DHCP Server is not installed in the local machine. GOTO Prereq GOTO:EOF @REM Stop the server if it is running. :DhcpServerInstalled echo DHCP Server Service is installed.. sc queryex dhcpserver | findstr STATE | findstr RUNNING 1>nul 2>nul if %ERRORLEVEL% == 0 goto DhcpServerStarted goto DhcpServerStopped :DhcpServerStarted echo DHCP Server Service is running.. echo Attempting to cleanup the Server Database.. net stop dhcpserver CALL:sleep 2 @REM Restore registry settings. (No paths are migrated - Refer prerequisite.) :DhcpServerStopped echo Restoring the DHCP specific Registry Settings. REG IMPORT %REGISTRY_EXPORT_FILE% SET LOG_FILE_REG= reg query HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\DHCPServer\Parameters /v DatabasePath FOR /F "usebackq delims==" %%z in (`%LOG_FILE_REG%`) do SET TEMP=%%z SET LOG_FILE_REG=%TEMP% for %%z in (%LOG_FILE_REG%) do SET DATABASE_FILE_PATH=%%z SET LOG_FILE_REG= reg query HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\DHCPServer\Parameters /v DhcpLogFilePath FOR /F "usebackq delims==" %%z in (`%LOG_FILE_REG%`) do SET TEMP=%%z SET LOG_FILE_REG=%TEMP% for %%z in (%LOG_FILE_REG%) do SET LOG_FILE_PATH=%%z SET LOG_FILE_REG= reg query HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\DHCPServer\Parameters /v DhcpV6LogFilePath FOR /F "usebackq delims==" %%z in (`%LOG_FILE_REG%`) do SET TEMP=%%z SET LOG_FILE_REG=%TEMP% for %%z in (%LOG_FILE_REG%) do SET LOGV6_FILE_PATH=%%z @REM Delete the existing database if -f is specified. if %FORCE%==1 ( echo Deleting stale database if any. del /q %DATABASE_FILE_PATH%\* ) @REM Restore the log files to the path found in registry. if %LOG_FILE_PATH%==%LOGV6_FILE_PATH% ( echo Copying the DHCP and DHCPV6 log files to the logs directory. copy Logs\Dhcp*.log %LOG_FILE_PATH% 1>nul 2>nul ) ELSE ( echo Copying the DHCP log files to the logs directory. copy Logs\Dhcp*.log %LOG_FILE_PATH% 1>nul 2>nul echo Copying the DHCPV6 log files to the logs directory. copy Logs\Dhcp*.log %LOGV6_FILE_PATH% 1>nul 2>nul ) @REM Start the DHCP Server. echo Starting the DHCP Server. net start dhcpserver IF errorlevel 0 ( echo DHCP Server Started Successfully. CALL:sleep 4 ) ELSE ( echo Problem starting DHCP Server. echo Reinstall the dhcp service and start importing again. GOTO:EOF ) @REM Import the exported settings. echo Importing settings from %DHCP_EXPORT_FILE%. netsh dhcp server import %DHCP_EXPORT_FILE% all 1>nul 2>nul @REM Detect conflict and bail-out with a message asking to use -f option. @REM If No conflict detected, display success. IF errorlevel 1 ( echo. echo Existing Server Database conflicts with the import settings. echo Try Force option -f to import after force delete of the current database. ) ELSE ( echo. echo Server Settings imported successfully. ) echo. popd GOTO:EOF @REM ---------------------------------------------------------------------- @REM Help Subroutine - Usage and Examples. :HELP echo. echo DhcpMig echo. echo This Help screen will be displayed if the options specified are either invalid echo or incomplete. echo. echo Please refer to the Usage and Examples below. echo. echo USAGE: echo. echo Prerequisite: DhcpMig -Prereq echo. echo - Displays Requirements before doing the Export/Import operations. echo - Recommended to read it once before proceeding. echo. echo Export: DhcpMig -export [-f] [Path] echo. echo [Path] is an optional Local/Remote location where the settings are stored. echo - If not specified, A default directory with name DHCP_MIG will be echo created in the current location. echo - It can also be a network fileshare location with write access to the echo current user. echo. echo [-f] is an optional force flag. echo - When the force flag is specified, If the [Path] specified is not echo empty, All contents will be deleted before exporting. echo - The server will be started for export operation if it in stopped state. echo. echo Import: DhcpMig -import [-f] [Path] echo. echo [Path] is an optional directory name where the settings will be retrived. echo - It should be same as the Path specified during Export operation. echo - It can also be a network fileshare location with read access to the echo current user. echo - If the default directory is used, It should be copied to the target echo machine before import. echo. echo [-f] is an optional force flag. echo - When the force flag is specified, the current database if any will echo be deleted before importing. echo - If the force flag is not specified, the databases will be "merged" echo and an error will be thrown in case of a collision. echo. echo. echo Examples: echo. echo Export: echo. echo DhcpMig -export echo - Exports settings and configurations to the default DHCP_MIG echo directory in the current location. echo. echo DhcpMig -export "\\<Fileshare>\dhcpconf\" echo - Exports setting and configurations to the specified local or shared echo directory. (dhcpconf in this case) echo - Please ensure that the directory specified has write permissions to echo the current user. echo. echo DhcpMig -export -f "\\<Fileshare>\dhcpconf\" echo - Forces deletion of all contents of target directory if it is non-empty. echo - Starts the DHCP Server inorder to complete the export operation if it echo is not running. echo - Exports setting and configurations to the specified local or shared echo directory. (dhcpconf in this case) echo. echo. echo Import: echo. echo DhcpMig -import echo - Imports settings and configurations from the default DHCP_MIG echo directory in the current location. echo - Throws an error if DHCP_MIG directory is not present in the current echo location. echo - Throws an error if the current server configuration conflicts with echo the settings being imported. Use -f if a conflict is reported. echo. echo DhcpMig -import -f echo - Deletes current server configurations if any. (FORCE flag Specified) echo - Imports settings and configurations from the default DHCP_MIG echo directory in the current location. echo - Throws an error if the DHCP_MIG directory is not present in the echo current location. echo. echo DhcpMig -import "\\<Fileshare>\dhcpconf\" echo - Imports settings and configurations from the specified local or shared echo directory. (dhcpconf in this case) echo - Throws an error if the specified directory is unavailable or echo unreachable. echo - Throws an error if the current server configuration conflicts with echo the settings being imported. Use -f if a conflict is reported. echo. echo DhcpMig -import -f "\\<Fileshare>\dhcpconf\" echo - Deletes current server configurations if any. (FORCE flag Specified) echo - Imports settings and configurations from the specified local or shared echo directory. (dhcpconf in this case) echo - Throws an error if the specified directory is unavailable or echo unreachable. echo. echo NOTE: The DHCP Server will be started and ready to give out addresses after the echo import operation completes. echo. GOTO:EOF @REM @REM Prerequisite Subroutine - Initial conditions for Import and Export. :Prereq echo. echo Please ensure the below Prerequisite before performing export/import operations. echo. echo EXPORT: echo. echo - The DHCP Server should be running on the local machine. echo - The Export path specified should be accessible from local machine. echo. echo IMPORT: echo. echo - The DHCP Server should be installed in the Local machine. echo - The Network addresses should be configured and server bound to the addresses. echo - The desired Paths for the Database, Backups and Audit Logs should be set as echo required. Ignore this step if you don't plan to change default paths. echo - The Database/Logs etc will be migrated to the paths mentioned. echo Database and Backups: echo Dhcpmgmt.msc -^> Select Server -^> Properties -^> To set. echo Audit logs: echo Dhcpmgmt.msc -^> Ipv4/Ipv6 -^> Properties -^> Advanced -^> To set. echo - The server startup should be set to "auto". echo SC Config DhcpServer start= auto echo - The Server can be Running or Stopped. echo. echo. echo NOTE: THE PATHS WILL NOT BE MIGRATED. ONLY THE CONFIGURED PATHS WILL BE USED. echo. GOTO:EOF @REM @REM Sleep Subroutine - Helper. :sleep set _x=%~1 set tick=0 set startTime=%time% set myMili=%startTime:~9,2% :newTick set /A tick=%tick%+1 set myTime=%time% set mySec=%myTime:~7,1% :newtime set newTime=%time% set twoSec=%newTime:~7,1% set twoMili=%newTime:~9,2% if %mySec% EQU %twoSec% (GOTO newtime) else GOTO nextTest :nextTest if %myMili% LEQ %twoMili%+1 (goto tick) else goto newtime :tick if %tick% NEQ %_x% goto newTick else GOTO:EOF @REM
Comments
0 B
|👍
/👎