Wednesday, August 14, 2013

Using Netsh to export and import DHCP reservations

Recently I had to move 80 or so virtual servers from one subnet to another.  The server's all have their IP's assigned via DHCP, and they keep them via DHCP reservations.  I also need to make the IP's reserved in the new scope.

This should be easy peasey.



I just need to use netsh to export the DHCP scope in question, do a find and replace to change the address, and re-import.

First, let's review the syntax for using the netsh export command.


  1. First you launch a netsh command prompt C:\Users\Administrator>netsh.exe
    netsh>
  2. Now that you have a Netsh command prompt, navigate to the DHCP commands.
    dhcp server \\servername
  3. Now issue the export command. The following command exports the full service configuration to the c:\Temp\Dhcpdb file:
    export c:\temp\dhcpdb all
    The following command exports the configuration that pertains to scopes 10.0.0.0 and 20.0.0.0 to the c:\Temp\Dhcpdb file:
    export c:\temp\dhcpdb 10.0.0.0 20.0.0.0

Wait a minute, there's a note on the netsh.exe dhcp command documentation:

Remarks
This command works only on the local server.
While the export command runs, the DHCP service is stopped and does not respond to DHCP clients seeking new leases or lease renewals.
If the DHCP service has a large number of scopes or a large number of client address leases, this command can take a long time to run.

Well, doesn't that just suck that the DHCP service has to stop.  Not quite as easy peasey as I hoped.

That leads us to the DUMP command:

dump
Dumps the configuration of the local DHCP server to the command prompt window when run within the netsh environment.

So using the DUMP command, you dump the entire config, and just copy the bits that you need, and reimport them.
So running the command (from an administrative command prompt):
C:\Users\Administrator>netsh dhcp server dump > dhcpcfg.txt
generates a file in that directory from the local dhcp server.

  • Note, you should be able to run the command remotely, but apparently there is/was a bug that prevented windows 7 and vista from accessing that command.  Works fine on XP and 2000 however. Go figure

Open up the file, and find the ReservedIP section for the scope you are interested in. It's very human readable, you shouldn't have any problem finding it.
The section of the file I wanted appears like this (of course I sanitized it):


# ======================================================================
#  Start Add ReservedIp to the Scope : 10.0.10.0, Server : 10.0.0.20            
# ======================================================================

Dhcp Server 10.0.0.20 Scope 10.0.0.0 Add reservedip 10.0.10.21 001122334455 "Server1" "" "DHCP" 
Dhcp Server \\DHCPSrv1 Scope 10.0.10.0 Add reservedip 10.0.10.22 001122334456 "Server2" "Web Server" "BOTH"
Dhcp Server \\DHCPSrv1.example.com Scope 10.0.10.0 Add reservedip 10.0.10.23 001122334457 "Server3" "ISA Server" "BOTH"

So I copy JUST this section to another blank text file so I can edit.

Let's cover the syntax:
The stuff surrounded in == and # are just comments, they're there to help a human read the file. Nothing more.
Dhcp Server 10.0.0.20
Dhcp Server \\DHCPSrv1
Dhcp Server \\DHCPSrv1.example.com
These are connection methods to identify the DHCP server, The first via IP, the second via NetBIOS, the third via FQDN.  Normally they would all be the same, I just did three to show the three different ways.
Scope 10.0.10.0 Add reservedip
Identifies the scope you want to edit, and what you want to do.
10.0.10.22 001122334456 "Server2" "Web Server"

The IP address you want to reserve, the MAC address it's bound to, The Reservation Name, the Reservation Description (Which is optional).

"BOTH"

Specifies the type of clients to associate with this reserved client entry. The options are {DHCP | BOOTP | BOTH}  DHCP is the default.

Now that you have your file all fixed up, you need to import it back in.
You have a few options.


  1. The easiest option is to launch the netsh.exe command from a command window, and just paste the file into the command window. This works best for small changes.
  2. Import the configuration changes using the netsh import command.  Crap, the import command stop's the DHCP server as well.
  3. Import the configuration changes using the netsh exec command.

I'm not going to cover the netsh import command, because it's going to stop the DHCP server.  Not good, I want to do this live, with no impact on my normal clients.

Use netsh exec to import the configuration changes
C:\Users\Administrator>netsh exec dhcpcfg-edited.txt

9 comments:

  1. Thanks for this post, together with http://social.technet.microsoft.com/Forums/windowsserver/en-US/4c0a2dc4-74b2-43ed-b05c-710c9c2e3b25/netsh-command-error-the-command-needs-a-valid-scope-ip-address?forum=winserverNIS this was really helping us to import our 20+ dhcp reservations from SBS 2003 to SBS 2011.

    ReplyDelete
  2. Excellent post, thanks very much. Saved me a lot of time

    ReplyDelete
  3. Thanks a lot for this mate, great job!

    ReplyDelete
  4. Great Information, but in the Agency I work, We the Hetwork Engineers are responcible for IPAM, we manage the DHCP servers via MMC access normally. The export and import commands only work when using netsh on the local host; when attempting to run the export from another server or my desktop I would get the following error:
    netsh dhcp server scope>export c:\temp\MyFile

    DHCP Server import and export are allowed on local servers only.
    Error 87 in FormatMessageW()

    Parameter(s) passed are either incomplete or invalid.
    netsh dhcp server scope>

    But you gave me enough to find the show reservedip command and the add reservedip commends to make it work.

    Just have to copy paste from the shell to text edit it and paste it back. Was only 100 reservations in a single scope, this time!

    ReplyDelete
    Replies
    1. There is a note about that about that about halfway thru my Blog post

      Delete
  5. Use this. from another site to automate it in a for loop

    for /f "tokens=1-6 delims=," %G IN (lists.txt) do netsh dhcp server \\server scope %G add reservedip %H %I %J %K %L

    ReplyDelete
  6. Thank you very much, for this guide! This helped me out immensely when I had to migrate DHCP scopes from a Server 2008 R2 server to a Server 2008 DHCP server!

    ReplyDelete
  7. Wonderful post! Saved me from recreating 65 scopes and hundreds of reservations!!! Thank you very much.

    ReplyDelete