IT Trenches

Jul 22 2009   2:55PM GMT

Using net shell to backup/restore DHCP

Troy Tate Profile: Troy Tate

Recently a fellow ITKE blogger posted Windows server 2003: DHCP server rant. I had been working on moving some DHCP services from one site to another when I saw his posting. I was using a series of network shell (netsh) commands to perform the necessary tasks to move scopes from one server to another. I thought I would share that series of commands with ITKE readers for your use.

We have about 20 sites with DHCP servers. So, this is a significant number of servers and scopes to manage. The Microsoft DHCP management console can be sluggish across a WAN so managing each server through RDP can be a pain also. Fortunately, the netsh dhcp server command set gives a good set of tools for managing DHCP services through a command line.

The netsh dhcp server help says:

Switches the active command context to the specified DHCP server.

Syntax:
dhcp> server <\\ServerName | ServerIP>

Parameters:
ServerName     – The NetBIOS or the DNS name of the DHCP server computer.
ServerIP       – The IP address of the DHCP server computer.

Notes:         If a value for neither ServerName nor ServerIP is specified, the local computer is assumed.

Examples:      server \\DHCP-SRV1
server \\dhcp-srv.microsoft.com
server 10.0.0.1

Using this command and the additional options available to manage DHCP, I wrote a script to go out to each DHCP server, make a copy of all scopes on that server and write it to a central location. The script is shown below. A prerequisite to the script is to get a copy of the psexec.exe tool from Sysinternals (Microsoft). I could use the remote server format of the command but prefer running the command on the remote servers’ command line.

The backup script is shown below. I named mine PS-dhcp-export.cmd

The first part of the script writes a batch file unique to each remote server.

:part1

if exist get-dhcp.cmd del get-dhcp.cmd
echo net use j: /d > get-dhcp.cmd
echo net use j: \\backupserver\c$ >> get-dhcp.cmd
echo j: >> get-dhcp.cmd
echo cd “\dhcpbackup” >> get-dhcp.cmd
echo netsh dhcp server export j:%1.txt all >> get-dhcp.cmd

Part2 actually runs the script on the remote computer and copies the get-dhcp.cmd file to the remote computer’s C:\ folder.

:part2
psexec -c -w c:\ get-dhcp.cmd \\%1

So, to run this against all remote site’s dhcp servers, I create a text file listing each remote authorized DHCP server. There is one server per line and looks something like this:

us1dc01
th6dc01
us2dc01
mx1dc01
us9dc01
us7dc02
uk3dc01
tw3dc01
us4dc01

This file is then read in and processed using the following backupdhcp.bat file:

for /F “tokens=*” %%I in (sites.txt) do call ps-dhcp-export.cmd %%I

Wait a few minutes and the backup is completed across all DHCP servers and a copy of the scopes is in one folder on another server.

If a scope needs restored or installed on a new DHCP server, use a command similar to the following:

netsh dhcp server import backupfile.txt scopesubnet

Where scopesubnet is either “ALL” or the subnet range like 10.2.0.0 or similar. More help can be found on the Microsoft Technet website for the netsh commands for DHCP.

Hope this tip has been useful. Thanks for reading & let’s continue to be good network citizens!

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: