Advertisement
dantpro

MSDHCP Server Migration

Mar 28th, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 15.00 KB | None | 0 0
  1. ::http://blogs.technet.com/b/teamdhcp/archive/2009/02/18/migration-of-dhcp-server-from-windows-server-2003-to-windows-server-2008.aspx
  2.  
  3. @REM DhcpMig.
  4. @ECHO OFF
  5. @REM
  6.  
  7.  
  8. @REM Variable decleration.
  9.  
  10. SET OPERATION=
  11. SET DEST=DHCP_MIG
  12.  
  13. SET DHCP_EXPORT_FILE=dhcp_export.exp
  14. SET REGISTRY_EXPORT_FILE=dhcp_reg_export.reg
  15. SET DATABASE_FILE_PATH=
  16. SET LOG_FILE_PATH=
  17. SET LOGV6_FILE_PATH=
  18. SET FORCE=0
  19. SET TEMP=
  20. SET DEST_SPECIFIED=0
  21.  
  22. SET Choice=N
  23. SET del_choice=N
  24.  
  25. @REM
  26.  
  27.  
  28. @REM Command line arguments validation.
  29.  
  30. IF (%1)==() (
  31.     GOTO HELP
  32. ) ELSE (
  33.     SET OPERATION=%1
  34. )
  35.  
  36. IF NOT (%2)==() IF (%2)==(-f) (
  37.     SET FORCE=1
  38. )
  39.  
  40. IF NOT (%2)==() IF NOT (%2)==(-f) (
  41.     SET DEST=%2
  42.     SET DEST_SPECIFIED=1
  43. )
  44.  
  45. IF NOT (%3)==() IF NOT (%2)==(-f) (
  46.     echo.
  47.     echo Wrong Arguments: Please refer the help below
  48.     GOTO HELP
  49. )
  50.  
  51. IF NOT (%3)==() (
  52.     SET DEST=%3
  53.     SET DEST_SPECIFIED=1
  54. )
  55.  
  56. echo.
  57.  
  58. @REM
  59.  
  60.  
  61. @REM Call Subroutine based on operation specified.
  62.  
  63. IF /I %OPERATION% == -export (
  64.     GOTO EXPORT
  65. ) ELSE IF /I %OPERATION% == -import (
  66.     GOTO IMPORT
  67. ) ELSE IF /I %OPERATION% == -prereq (
  68.     GOTO Prereq
  69. ) ELSE (
  70.     GOTO HELP
  71. )
  72.  
  73. @REM ----------------------------------------------------------------------
  74.  
  75.  
  76. @REM Export Subroutine.
  77. @REM
  78.  
  79. :EXPORT
  80.  
  81. @REM Validate if server is installed.
  82.  
  83. sc queryex dhcpserver | findstr /i /c:"EnumQueryServicesStatus:OpenService FAILED 1060" >nul 2>nul
  84. if %ERRORLEVEL% == 0 (
  85.     echo ERROR:Dhcp server does not seen to have been installed on this machine.
  86.     echo       Please run the script on a correct server.
  87.     GOTO:EOF
  88. )
  89.  
  90. @REM Validate if server is running. Start if -f is specified else bail-out.
  91.  
  92. sc queryex dhcpserver | findstr STATE | findstr RUNNING 1>nul 2>nul
  93. if not %ERRORLEVEL% == 0 (
  94.     IF %FORCE% == 0 (
  95.         echo Dhcp Server is not running on the Local machine.
  96.         echo Specify -f option to start the Server and perform an export.
  97.         GOTO:EOF
  98.     )
  99.     IF %FORCE% == 1 (
  100.         net start dhcpserver
  101.     )
  102. )
  103.  
  104. @REM Set Log file paths by reading the registry.
  105.  
  106. SET LOG_FILE_REG= reg query HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\DHCPServer\Parameters /v DhcpLogFilePath
  107. FOR /F "usebackq delims==" %%z in (`%LOG_FILE_REG%`) do SET TEMP=%%z
  108. SET LOG_FILE_REG=%TEMP%
  109. for %%z in (%LOG_FILE_REG%) do SET LOG_FILE_PATH=%%z
  110.  
  111. SET LOG_FILE_REG= reg query HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\DHCPServer\Parameters /v DhcpV6LogFilePath
  112. FOR /F "usebackq delims==" %%z in (`%LOG_FILE_REG%`) do SET TEMP=%%z
  113. SET LOG_FILE_REG=%TEMP%
  114. for %%z in (%LOG_FILE_REG%) do SET LOGV6_FILE_PATH=%%z
  115.  
  116.  
  117. @REM Create the destination directory if it does not exist.
  118.  
  119. IF %DEST_SPECIFIED% == 0 (
  120.     echo No destination directory specified.
  121.     echo Settings will be exported to %DEST% in the Local directory.
  122. )
  123.  
  124. if not exist %DEST%\* (
  125.     md %DEST%
  126.     IF not errorlevel 0 (
  127.         echo Cannot create "%DEST%" directory. Please check permissions.
  128.     ) ELSE (
  129.         echo Successfully created "%DEST%" directory.
  130.     )
  131. )
  132.  
  133.  
  134. @REM Validate if the destination directory is empty.
  135. @REM If -f is specified, delete the contents else bail-out.
  136.  
  137. if exist %DEST%\dhcp*.* (
  138.     echo Directory %DEST% is not empty.
  139.     IF %FORCE% == 0 (
  140.         echo Backup the files and start again or
  141.         echo Please specify a different directory or
  142.         echo Specify '-f' option to overwrite directory contents.
  143.         GOTO:EOF
  144.     )
  145.     IF %FORCE% == 1 (
  146.         echo Force deleting the contents of directory %DEST%
  147.         pushd %DEST%
  148.         del /q *.* 1>nul 2>nul
  149.         if exist Logs (
  150.             pushd Logs
  151.             del /q *.* 1>nul 2>nul
  152.             popd
  153.             rmdir Logs
  154.         )
  155.         popd
  156.     )
  157. )
  158.  
  159.  
  160. @REM Invoke Export command.
  161.  
  162. pushd %DEST%
  163.  
  164. echo Exporting Server configurations to the file %DHCP_EXPORT_FILE%
  165. netsh dhcp server export %DHCP_EXPORT_FILE% all 1>nul 2>nul
  166.  
  167. @REM Export registry settings.
  168.  
  169. echo Exporting registry settings to %REGISTRY_EXPORT_FILE%
  170. REG EXPORT HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\DHCPServer\Parameters %REGISTRY_EXPORT_FILE%
  171.  
  172. @REM Remove all "Paths" from the registry export.. (Paths will not be migrated)
  173.  
  174. type %REGISTRY_EXPORT_FILE% | findstr /vi path > temp.reg
  175. del %REGISTRY_EXPORT_FILE%
  176. ren temp.reg %REGISTRY_EXPORT_FILE%
  177.  
  178.  
  179. @REM Copy the log files from the location specified in the registry.
  180.  
  181. md Logs
  182.  
  183. if %LOG_FILE_PATH%==%LOGV6_FILE_PATH% (
  184.     echo Backing up Log Files from %LOG_FILE_PATH%
  185.     copy %LOG_FILE_PATH%\Dhcp*.log logs 1>nul 2>nul
  186. ) ELSE (
  187.     echo Backing up DHCP Log Files from %LOG_FILE_PATH%
  188.     copy %LOG_FILE_PATH%\Dhcp*.log logs 1>nul 2>nul
  189.  
  190.     echo Backing up DHCPV6 Log Files from %LOGV6_FILE_PATH%
  191.     copy %LOGV6_FILE_PATH%\Dhcp*.log logs 1>nul 2>nul
  192. )
  193.  
  194. echo.
  195. echo Settings exported successfully.
  196.  
  197. popd
  198.  
  199. GOTO:EOF
  200.  
  201. @REM ----------------------------------------------------------------------
  202.  
  203.  
  204. @REM Import Subroutine.
  205. @REM
  206.  
  207. :IMPORT
  208.  
  209. @REM Validate source directory and bail-out if it does not exist.
  210.  
  211. if not exist %DEST%\* (
  212.     echo The directory %DEST% does not exist or is not accessible.
  213.     echo.
  214.     echo Please provide a correct directory and start again.
  215.     popd
  216.     GOTO:EOF
  217. )
  218.  
  219. @REM Validate individual files and bail-out if they do not exist.
  220.  
  221. pushd %DEST%
  222.  
  223. if not exist %DHCP_EXPORT_FILE% (
  224.     echo %DEST%\%DHCP_EXPORT_FILE% file does not exist or is not accessible.
  225.     echo.
  226.     echo Please provide a correct directory and start again.
  227.     popd
  228.     GOTO:EOF
  229. )
  230.  
  231. if not exist %REGISTRY_EXPORT_FILE% (
  232.     echo %DEST%\%REGISTRY_EXPORT_FILE% file does not exist or is not accessible.
  233.     echo.
  234.     echo Please provide a correct directory and start again.
  235.     popd
  236.     GOTO:EOF
  237. )
  238.  
  239. if not exist Logs\* (
  240.     echo Directory %DEST% does not contain the "Logs" directory.
  241.     echo.
  242.     echo Please verify and provide a correct directory and start again.
  243.     popd
  244.     GOTO:EOF
  245. )
  246.  
  247. @REM Check if DHCP Server is installed. Bail-out if not.
  248.  
  249. sc queryex dhcpserver | findstr /i /c:"EnumQueryServicesStatus:OpenService FAILED 1060" >nul 2>nul
  250. if not %ERRORLEVEL% == 0 goto DhcpServerInstalled
  251. echo DHCP Server is not installed in the local machine.
  252. GOTO Prereq
  253. GOTO:EOF
  254.  
  255. @REM Stop the server if it is running.
  256.  
  257. :DhcpServerInstalled
  258. echo DHCP Server Service is installed..
  259. sc queryex dhcpserver | findstr STATE | findstr RUNNING 1>nul 2>nul
  260. if %ERRORLEVEL% == 0 goto DhcpServerStarted
  261. goto DhcpServerStopped
  262.  
  263.  
  264. :DhcpServerStarted
  265. echo DHCP Server Service is running..
  266. echo Attempting to cleanup the Server Database..
  267. net stop dhcpserver
  268. CALL:sleep 2
  269.  
  270. @REM Restore registry settings. (No paths are migrated - Refer prerequisite.)
  271. :DhcpServerStopped
  272. echo Restoring the DHCP specific Registry Settings.
  273. REG IMPORT %REGISTRY_EXPORT_FILE%
  274.  
  275. SET LOG_FILE_REG= reg query HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\DHCPServer\Parameters /v DatabasePath
  276. FOR /F "usebackq delims==" %%z in (`%LOG_FILE_REG%`) do SET TEMP=%%z
  277. SET LOG_FILE_REG=%TEMP%
  278. for %%z in (%LOG_FILE_REG%) do SET DATABASE_FILE_PATH=%%z
  279.  
  280. SET LOG_FILE_REG= reg query HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\DHCPServer\Parameters /v DhcpLogFilePath
  281. FOR /F "usebackq delims==" %%z in (`%LOG_FILE_REG%`) do SET TEMP=%%z
  282. SET LOG_FILE_REG=%TEMP%
  283. for %%z in (%LOG_FILE_REG%) do SET LOG_FILE_PATH=%%z
  284.  
  285. SET LOG_FILE_REG= reg query HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\DHCPServer\Parameters /v DhcpV6LogFilePath
  286. FOR /F "usebackq delims==" %%z in (`%LOG_FILE_REG%`) do SET TEMP=%%z
  287. SET LOG_FILE_REG=%TEMP%
  288. for %%z in (%LOG_FILE_REG%) do SET LOGV6_FILE_PATH=%%z
  289.  
  290. @REM Delete the existing database if -f is specified.
  291.  
  292. if %FORCE%==1 (
  293.     echo Deleting stale database if any.
  294.     del /q %DATABASE_FILE_PATH%\*
  295. )
  296.  
  297. @REM Restore the log files to the path found in registry.
  298.  
  299. if %LOG_FILE_PATH%==%LOGV6_FILE_PATH% (
  300.     echo Copying the DHCP and DHCPV6 log files to the logs directory.
  301.     copy Logs\Dhcp*.log %LOG_FILE_PATH% 1>nul 2>nul
  302. ) ELSE (
  303.     echo Copying the DHCP log files to the logs directory.
  304.     copy Logs\Dhcp*.log %LOG_FILE_PATH% 1>nul 2>nul
  305.  
  306.     echo Copying the DHCPV6 log files to the logs directory.
  307.     copy Logs\Dhcp*.log %LOGV6_FILE_PATH% 1>nul 2>nul
  308. )
  309.  
  310. @REM Start the DHCP Server.
  311.  
  312. echo Starting the DHCP Server.
  313. net start dhcpserver
  314. IF errorlevel 0 (
  315.     echo DHCP Server Started Successfully.
  316.     CALL:sleep 4
  317. ) ELSE (
  318.     echo Problem starting DHCP Server.
  319.         echo Reinstall the dhcp service and start importing again.
  320.     GOTO:EOF
  321. )
  322.  
  323. @REM Import the exported settings.
  324.  
  325. echo Importing settings from %DHCP_EXPORT_FILE%.
  326. netsh dhcp server import %DHCP_EXPORT_FILE% all 1>nul 2>nul
  327.  
  328. @REM Detect conflict and bail-out with a message asking to use -f option.
  329. @REM If No conflict detected, display success.
  330. IF errorlevel 1 (
  331.     echo.
  332.     echo Existing Server Database conflicts with the import settings.
  333.     echo Try Force option -f to import after force delete of the current database.
  334. ) ELSE (
  335.     echo.
  336.     echo Server Settings imported successfully.
  337. )
  338.  
  339. echo.
  340.  
  341. popd
  342.  
  343. GOTO:EOF
  344.  
  345. @REM ----------------------------------------------------------------------
  346.  
  347.  
  348. @REM Help Subroutine - Usage and Examples.
  349.  
  350. :HELP
  351.  
  352. echo.
  353. echo DhcpMig
  354. echo.
  355. echo This Help screen will be displayed if the options specified are either invalid
  356. echo or incomplete.
  357. echo.
  358. echo Please refer to the Usage and Examples below.
  359. echo.
  360. echo USAGE:
  361. echo.
  362. echo Prerequisite: DhcpMig -Prereq
  363. echo.
  364. echo        - Displays Requirements before doing the Export/Import operations.
  365. echo        - Recommended to read it once before proceeding.
  366. echo.
  367. echo Export:  DhcpMig -export [-f] [Path]
  368. echo.
  369. echo    [Path] is an optional Local/Remote location where the settings are stored.
  370. echo        - If not specified, A default  directory with name DHCP_MIG will be
  371. echo          created in the current location.
  372. echo        - It can also be a network fileshare location with write access to the
  373. echo          current user.
  374. echo.
  375. echo    [-f] is an optional force flag.
  376. echo        - When the force flag is specified, If the [Path] specified is not  
  377. echo          empty, All contents will be deleted before exporting.
  378. echo        - The server will be started for export operation if it in stopped state.
  379. echo.
  380. echo Import:  DhcpMig -import [-f] [Path]
  381. echo.
  382. echo    [Path] is an optional directory name where the settings will be retrived.
  383. echo        - It should be same as the Path specified during Export operation.
  384. echo        - It can also be a network fileshare location with read access to the
  385. echo          current user.
  386. echo        - If the default directory is used, It should be copied to the target
  387. echo          machine before import.
  388. echo.
  389. echo    [-f] is an optional force flag.
  390. echo        - When the force flag is specified, the current database if any will
  391. echo          be deleted before importing.
  392. echo        - If the force flag is not specified, the databases will be "merged"
  393. echo          and an error will be thrown in case of a collision.
  394. echo.
  395. echo.
  396. echo Examples:
  397. echo.
  398. echo Export:
  399. echo.
  400. echo    DhcpMig -export
  401. echo        - Exports settings and configurations to the default DHCP_MIG
  402. echo          directory in the current location.
  403. echo.
  404. echo    DhcpMig -export "\\<Fileshare>\dhcpconf\"
  405. echo        - Exports setting and configurations to the specified local or shared
  406. echo          directory.  (dhcpconf in this case)
  407. echo        - Please ensure that the directory specified has write permissions to
  408. echo          the current user.
  409. echo.
  410. echo    DhcpMig -export -f "\\<Fileshare>\dhcpconf\"
  411. echo        - Forces deletion of all contents of target directory if it is non-empty.
  412. echo        - Starts the DHCP Server inorder to complete the export operation if it
  413. echo          is not running.
  414. echo        - Exports setting and configurations to the specified local or shared
  415. echo          directory.  (dhcpconf in this case)
  416. echo.
  417. echo.
  418. echo Import:
  419. echo.
  420. echo    DhcpMig -import
  421. echo        - Imports settings and configurations from the default DHCP_MIG
  422. echo          directory in the current location.
  423. echo        - Throws an error if DHCP_MIG directory is not present in the current
  424. echo          location.
  425. echo        - Throws an error if the current server configuration conflicts with
  426. echo          the settings being imported. Use -f if a conflict is reported.
  427. echo.
  428. echo    DhcpMig -import -f
  429. echo        - Deletes current server configurations if any. (FORCE flag Specified)
  430. echo        - Imports settings and configurations from the default DHCP_MIG
  431. echo          directory in the current location.
  432. echo        - Throws an error if the DHCP_MIG directory is not present in the
  433. echo          current location.
  434. echo.
  435. echo    DhcpMig -import "\\<Fileshare>\dhcpconf\"
  436. echo        - Imports settings and configurations from the specified local or shared
  437. echo          directory. (dhcpconf in this case)
  438. echo        - Throws an error if the specified directory is unavailable or
  439. echo          unreachable.
  440. echo        - Throws an error if the current server configuration conflicts with
  441. echo          the settings being imported. Use -f if a conflict is reported.
  442. echo.
  443. echo    DhcpMig -import -f "\\<Fileshare>\dhcpconf\"
  444. echo        - Deletes current server configurations if any. (FORCE flag Specified)
  445. echo        - Imports settings and configurations from the specified local or shared
  446. echo          directory. (dhcpconf in this case)
  447. echo        - Throws an error if the specified directory is unavailable or
  448. echo          unreachable.
  449. echo.
  450. echo NOTE: The DHCP Server will be started and ready to give out addresses after the
  451. echo       import operation completes.
  452. echo.
  453.  
  454. GOTO:EOF
  455.  
  456. @REM
  457.  
  458.  
  459. @REM Prerequisite Subroutine - Initial conditions for Import and Export.
  460.  
  461.  
  462. :Prereq
  463.  
  464. echo.
  465. echo Please ensure the below Prerequisite before performing export/import operations.
  466. echo.
  467. echo EXPORT:
  468. echo.
  469. echo   - The DHCP Server should be running on the local machine.
  470. echo   - The Export path specified should be accessible from local machine.
  471. echo.
  472. echo IMPORT:
  473. echo.
  474. echo   - The DHCP Server should be installed in the Local machine.
  475. echo   - The Network addresses should be configured and server bound to the addresses.
  476. echo   - The desired Paths for the Database, Backups and Audit Logs should be set as
  477. echo         required. Ignore this step if you don't plan to change default paths.
  478. echo   - The Database/Logs etc will be migrated to the paths mentioned.
  479. echo        Database and Backups:
  480. echo            Dhcpmgmt.msc -^> Select Server -^> Properties -^> To set.
  481. echo        Audit logs:
  482. echo            Dhcpmgmt.msc -^> Ipv4/Ipv6 -^> Properties -^> Advanced -^> To set.
  483. echo   - The server startup should be set to "auto".
  484. echo        SC Config DhcpServer start= auto
  485. echo   - The Server can be Running or Stopped.
  486. echo.
  487. echo.
  488. echo   NOTE: THE PATHS WILL NOT BE MIGRATED. ONLY THE CONFIGURED PATHS WILL BE USED.                
  489. echo.
  490.  
  491. GOTO:EOF
  492.  
  493.  
  494. @REM
  495.  
  496.  
  497. @REM Sleep Subroutine - Helper.
  498.  
  499. :sleep
  500.  
  501. set _x=%~1
  502. set tick=0
  503. set startTime=%time%
  504. set myMili=%startTime:~9,2%
  505.  
  506. :newTick
  507. set /A tick=%tick%+1
  508. set myTime=%time%
  509. set mySec=%myTime:~7,1%
  510.  
  511. :newtime
  512. set newTime=%time%
  513. set twoSec=%newTime:~7,1%
  514. set twoMili=%newTime:~9,2%
  515.  
  516. if %mySec% EQU %twoSec% (GOTO newtime) else GOTO nextTest
  517.  
  518. :nextTest
  519. if %myMili% LEQ %twoMili%+1 (goto tick) else goto newtime
  520. :tick
  521.  
  522. if %tick% NEQ %_x% goto newTick else GOTO:EOF
  523.  
  524. @REM
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement