Posted by: Troy Tate
bach files, backup, command line, command line dhcp, dhcp, dhcp management, network shell, restore, server management
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.
dhcp> server <\\ServerName | ServerIP>
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
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.
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.
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:
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!