Aussie owned and operated, with 23+ years of industry experience, Gold Coast InfoTech is delighted to support all of your business technology requirements.

Gallery

Contacts

success@goldcoastinfotech.com.au

1800 291 071

KBArticles Scripts, Commands, Registry Uncategorized

How to Import and Export DHCP Reservations with Microsoft DHCP and netsh.exe

I had a senario the other day where I needed to synchronise the DHCP reservations on one W2008 domain controller to another W2008 for the same scopes (but each DC offered differnet IP’s from those scopes as per Microsoft’s 80/20 rule)

Microsoft offers the netsh command from the command line to make life easier for us poor I.T admins, make sure you run the command prompt as ADMINISTRATOR>

ISSUES:

  1. A complete DHCP server export cannot be imported onto another server where those scope, or DHCP options already exist. (have to delete the DB first and start again)
  2. The netsh export and import commands can only be run from the server itself (or running psexec.exe)
  3. The ‘netsh export all’ cannot just be export reservations and only exports in binary
  4. The netsh exec command will NOT work with the servername or localhost, but will work with 127.0.0.1 (provided DHCP server is listening on all IP’s and not statically set to listen on 1 IP only)
  5. Importing reservations will only update those that do not conflict – if there is a conflict the importing machine will NOT change its record.

There may be other ways to utilise the export command and delineate between scopes and options etc, but I had neither the time nor the inclination to figure it out. 🙂

STEP BY STEP
The following tasks were utilised to export reservations from DC1 to DC2

Dump the database as a ‘script file’ from the DHCP server (instead of an export) from the DC1 Server
netsh dhcp server dump > c:\server1_dump.txt

Export just the reservations from c:server1_dump.txt (leaves the first line as garbage info, but netsh can skip past this anyway)
find /i “add reservedip” c:\server1_dump.txt | find /V “#” > c:\server1_reservations.txt

Change the c:server1_reservations.txt to match the importing server’s IP addresses (manually find and replace the IPs listed in the file) or run the below VBSCRIPT

Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objFile = objFSO.OpenTextFile(“c:server1_reservations.txt”, ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, “DC1 “, “DC2 “)

‘NOTE THE SPACE AFTER SERVER NAMES
Set objFile = objFSO.OpenTextFile(“c:server1_reservations.txt”, ForWriting)
objFile.WriteLine strNewText
objFile.Close

Import the new file on the DC2 server
netsh exec c:server1_reservations.txt

Rudimentary but it works… 🙂

UPDATE NOTE:

Windows 2003 would export the server as its IP Address – so the find and replace string above would work based on IP – in window s2008 it exports the FQDN of the server – so the script must be changed to change the fqdn of the first server with the second.

RUNNING AS BATCH FILE (Elevated Rights & UAC):

I simply added this to the Windows 2008 task scheduler with ‘run as highest privileges’ and to run as a user that had the logon as a batch job right.

FULL IMPORT FROM A FULL EXPORT COMMAND: (we ran this on a ‘standby server’ at our DR site incase the two primary DHCP servers failed)

net stop “dhcp server”
del “C:\windows\system32\dhcp\dhcp.old” /Q
rename “C:\windows\system32\dhcp\dhcp.mdb” dhcp.old
net start “dhcp server”
netsh dhcp server import “DHCPSERVEREXPORTLOCATION\SERVER_export.txt”

Author

Customer

Comment (1)

  1. kingknob
    5 June 2013

    This is awsome, I wish we had something like that where I work, we asked one of the senior engineers (also called James incidently) to complete this a while back, I will be sure to point him to this URL

Leave a comment

Your email address will not be published. Required fields are marked *