Solved

Export DHCP from Server 2003 to 2008

Posted on 2014-02-03
8
675 Views
Last Modified: 2014-02-09
Hi Guys, i have so many of these to do over the next few weeks so im trying to automate as much as possible. Essentially im looking to build a script with some variables where server name is. Ive built some nice scripts with the help of some of the guys on here, but this is different and may not be possible. is there a way to run netsh through powershell other that start-process cmd?

thanks

Export the DHCP database from Windows 2003:

1.                   On the Windows 2003 DHCP server, navigate to a command prompt

2.                   Type the following Command: netsh

3.                   Type the following Command: DHCP

4.                   Type the following Command: server <\\Name or IP Address>

5.                   Type the following Command: export c:\w2k3DHCPdb all

Note You must have local administrator permissions to export the data.

Import the DHCP database

1.       Copy the exported DHCP database file to the local hard disk of the Windows Server 2008-based computer.

2.       Install the DHCP Role on the server.

3.       Stop the DHCP server service on the server.  To do this, follow these steps:

a.       Log on to the target DHCP server by using an account that is a member of the local Administrators group.

b.      Click Start, click Run, type cmd in the Open box, and then click OK.

c.       At the command prompt, type net stop DHCPserver , and then press ENTER. You receive a "The Microsoft DHCP Server service is stopping. The Microsoft DHCP Server service was stopped successfully" message.

d.      Type exit, and then press ENTER.

4.       Delete the DHCP.mdb file under c:\windows\system32\DHCP folder.

5.       Start the DHCP server service.

6.       Right-click on the Command Prompt (cmd) and select run as administrator, to open the cmd prompt using elevated privileges.
0
Comment
Question by:cwstad2
  • 4
  • 4
8 Comments
 
LVL 70

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 39832508
Why aren't you using the method described here instead of playing with the DHCP database file?

http://blogs.technet.com/b/networking/archive/2008/06/27/steps-to-move-a-dhcp-database-from-a-windows-server-2003-or-2008-to-another-windows-server-2008-machine.aspx

netsh can be run as-is in PowerShell. That means all you need is one line for each side:
param(
  [Parameter(Mandatory = $true)]
  [String]$SourceServer = "localhost",

  [Parameter(Mandatory = $true)]
  [String]$DestinationServer,

  [String]$ExportPath = "dhcpexport"
)

# Ensure all paths we wish to use exist
$Source = "\\$SourceServer\c$\$ExportPath"
if (-not (Test-Path $Source)) {
  New-Item $Source -ItemType Directory | Out-Null
}
$Destination = "\\$DestinationServer\c$\$ExportPath"
if (-not (Test-Path $Destination)) {
  New-Item $Destination -ItemType Directory | Out-Null
}

# Export the database
netsh dhcp "\\$SourceServer" export "c:\$ExportPath\dhcpserver.txt" all
# Copy the database file
Copy-Item "$Source\dhcpserver.txt" "$Destination\dhcpserver.txt"
# Import the database - DHCP must be installed. 
# Add-WindowsFeature could be used to do that if you were to use Remoting.
netsh dhcp "\\$DestinationServer" import "c:\$ExportPath\dhcpserver.txt" all

Open in new window

You may add a little cleanup to kill off the created folder and, now redundant, DHCP export file.

HTH

Chris
0
 
LVL 15

Author Comment

by:cwstad2
ID: 39833833
Hi Chris, thanks for your reply. Very detailed. I ran the script and got the following

thanks

param(
  [Parameter(Mandatory = $true)]
  [String]$SourceServer = "localhost",

  [Parameter(Mandatory = $true)]
  [String]$DestinationServer,

  [String]$ExportPath = "dhcpexport"
)

# Ensure all paths we wish to use exist
$Source = "\\$SourceServer\c$\$ExportPath"
if (-not (Test-Path $Source)) {
  New-Item $Source -ItemType Directory | Out-Null
}
$Destination = "\\$DestinationServer\c$\$ExportPath"
if (-not (Test-Path $Destination)) {
  New-Item $Destination -ItemType Directory | Out-Null
}

# Export the database
netsh dhcp "\\$SourceServer" export "c:\$ExportPath\dhcpserver.txt" all
# Copy the database file
Copy-Item "$Source\dhcpserver.txt" "$Destination\dhcpserver.txt"
# Import the database - DHCP must be installed.
# Add-WindowsFeature could be used to do that if you were to use Remoting.
netsh dhcp "\\$DestinationServer" import "c:\$ExportPath\dhcpserver.txt" all
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 39833876
I noticed a few mistakes now you re-post it. Specifically I missed the "server" command out of the netsh call.
param(
  [Parameter(Mandatory = $true)]
  [String]$SourceServer = "localhost",

  [Parameter(Mandatory = $true)]
  [String]$DestinationServer,

  [String]$ExportPath = "dhcpexport"
)

# Ensure all paths we wish to use exist
$Source = "\\$SourceServer\c$\$ExportPath"
if (-not (Test-Path $Source)) {
  New-Item $Source -ItemType Directory | Out-Null
}
$Destination = "\\$DestinationServer\c$\$ExportPath"
if (-not (Test-Path $Destination)) {
  New-Item $Destination -ItemType Directory | Out-Null
}

# Export the database
netsh dhcp server "\\$SourceServer" export "c:\$ExportPath\dhcpserver.txt" all
# Copy the database file
Copy-Item "$Source\dhcpserver.txt" "$Destination\dhcpserver.txt"
# Import the database - DHCP must be installed.
# Add-WindowsFeature could be used to do that if you were to use Remoting.
netsh dhcp server "\\$DestinationServer" import "c:\$ExportPath\dhcpserver.txt" all 

Open in new window

Is it throwing errors at you after this change?

Chris
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 15

Author Comment

by:cwstad2
ID: 39834849
Hi Chris, i get the following error. Am i missing something? Should i change originating server and destination?

PS C:\Documents and Settings\Administrator.TEST> param(
  [Parameter(Mandatory = $true)]
  [String]$SourceServer = "localhost",

  [Parameter(Mandatory = $true)]
  [String]$DestinationServer,

  [String]$ExportPath = "dhcpexport"
)

# Ensure all paths we wish to use exist
$Source = "\\$SourceServer\c$\$ExportPath"
if (-not (Test-Path $Source)) {
  New-Item $Source -ItemType Directory | Out-Null
}
$Destination = "\\$DestinationServer\c$\$ExportPath"
if (-not (Test-Path $Destination)) {
  New-Item $Destination -ItemType Directory | Out-Null
}

# Export the database
netsh dhcp "\\$SourceServer" export "c:\$ExportPath\dhcpserver.txt" all
# Copy the database file
Copy-Item "$Source\dhcpserver.txt" "$Destination\dhcpserver.txt"
# Import the database - DHCP must be installed.
# Add-WindowsFeature could be used to do that if you were to use Remoting.
netsh dhcp "\\$DestinationServer" import "c:\$ExportPath\dhcpserver.txt" all
New-Item : The path is not of a legal form.
At line:18 char:11
+   New-Item <<<<  $Destination -ItemType Directory | Out-Null
    + CategoryInfo          : InvalidArgument: (\\\c$\dhcpexport:String) [New-Item], ArgumentException
    + FullyQualifiedErrorId : CreateDirectoryArgumentError,Microsoft.PowerShell.Commands.NewItemCommand
 
The following command was not found: dhcp \\localhost export c:\dhcpexport\dhcpserver.txt all.
Copy-Item : Cannot find path '\\localhost\c$\dhcpexport\dhcpserver.txt' because it does not exist.
At line:24 char:10
+ Copy-Item <<<<  "$Source\dhcpserver.txt" "$Destination\dhcpserver.txt"
    + CategoryInfo          : ObjectNotFound: (\\localhost\c$\dhcpexport\dhcpserver.txt:String) [Copy-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
 
The following command was not found: dhcp \\ import c:\dhcpexport\dhcpserver.txt all.

____________________________________________________________________________________________________________________________________
PS C:\Documents and Settings\Administrator.TEST> param(
  [Parameter(Mandatory = $true)]
  [String]$SourceServer = "localhost",

  [Parameter(Mandatory = $true)]
  [String]$DestinationServer,

  [String]$ExportPath = "dhcpexport"
)

# Ensure all paths we wish to use exist
$Source = "\\$SourceServer\c$\$ExportPath"
if (-not (Test-Path $Source)) {
  New-Item $Source -ItemType Directory | Out-Null
}
$Destination = "\\$DestinationServer\c$\$ExportPath"
if (-not (Test-Path $Destination)) {
  New-Item $Destination -ItemType Directory | Out-Null
}

# Export the database
netsh dhcp server "\\$SourceServer" export "c:\$ExportPath\dhcpserver.txt" all
# Copy the database file
Copy-Item "$Source\dhcpserver.txt" "$Destination\dhcpserver.txt"
# Import the database - DHCP must be installed.
# Add-WindowsFeature could be used to do that if you were to use Remoting.
netsh dhcp server "\\$DestinationServer" import "c:\$ExportPath\dhcpserver.txt" all
                                           
New-Item : The path is not of a legal form.
At line:18 char:11
+   New-Item <<<<  $Destination -ItemType Directory | Out-Null
    + CategoryInfo          : InvalidArgument: (\\\c$\dhcpexport:String) [New-Item], ArgumentException
    + FullyQualifiedErrorId : CreateDirectoryArgumentError,Microsoft.PowerShell.Commands.NewItemCommand
 

Command completed successfully.
Copy-Item : The network path was not found.

At line:24 char:10
+ Copy-Item <<<<  "$Source\dhcpserver.txt" "$Destination\dhcpserver.txt"
    + CategoryInfo          : NotSpecified: (:) [Copy-Item], IOException
    + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.CopyItemCommand
 

Error while importing subnet 192.168.1.0 "server2003."
This subnet already exists on the local server.

____________________________________________________________________________________________________________________________________
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 39834921
You're leaving source and destination server blank. The params section will only take effect if the content is saved as a script. If you wish to paste that's fine, we just need a function.

I've taken the opportunity to add Get-Help information ("Get-Help Copy-DhcpServer" after you paste it in), a small amount of error condition checking and a few more comments,
function Copy-DhcpServer {
  # .SYNOPSIS
  #   Copies DHCP server configuration and leases from one server to another using NetSh.
  # .DESCRIPTION
  #   Copy-DhcpServer uses the import and export functionality built into NetSh to copy DHCP server configuration between servers.
  #
  #   Warning: This script has little (or no) sanity checking. Ensure you are providing reasonable values for all parameters.
  # .PARAMETER SourceServer
  #   The Windows 2003 or 2008 server from which you wish to export DHCP configuration.
  # .PARAMETER DestinationServer
  #   The Windows 2008 server you wish to import DHCP configuration into.
  # .PARAMETER ExportPath
  #   A local path, relative to C: on each server. By default, DHCP data will be exported to C:\dhcpexport, copied across to the same directory on the destination server, then imported.
  # .INPUTS
  #   System.String
  # .EXAMPLE
  #   Copy-DhcpServer -DestinationServer NewServer
  #
  #   Copies the DHCP server configuration from the local system to NewServer.
  # .EXAMPLE
  #   Copy-DhcpServer OldServer NewServer
  #
  #   Copies DHCP server configuration from OldServer to NewServer.  
 
  [CmdLetBinding()]
  param(
    [Parameter(Mandatory = $true, Position = 1)]
    [String]$SourceServer = "localhost",
  
    [Parameter(Mandatory = $true, Position = 2)]
    [String]$DestinationServer,
  
    [String]$ExportPath = "dhcpexport"
  )
  
  # Verify source and destination are not the same. There are still ways to fool this, but it's a reasonable start.
  if ($SourceServer -eq $DestinationServer -or $SourceServer -match "\.|localhost|$(hostname)" -and $DestinationServer -match "\.|localhost|$(hostname)") {
    Write-Error "Unable to move DHCP configuration. Source and destination are the same." -Category InvalidArgument
    break
  }
  
  # Ensure all paths we wish to use exist
  $Source = "\\$SourceServer\c$\$ExportPath"
  if (-not (Test-Path $Source)) {
    New-Item $Source -ItemType Directory | Out-Null
  }
  $Destination = "\\$DestinationServer\c$\$ExportPath"
  if (-not (Test-Path $Destination)) {
    New-Item $Destination -ItemType Directory | Out-Null
  }
  
  # Export DHCP configuration
  Write-Verbose "Exporting DHCP server configuration from $SourceServer"
  netsh dhcp server "\\$SourceServer" export "c:\$ExportPath\dhcpserver.txt" all

  # Copy the database file to DestinationServer
  Copy-Item "$Source\dhcpserver.txt" "$Destination\dhcpserver.txt"
  # Verify the file exists before importing.
  if (Test-Path "$Destination\dhcpserver.txt") {
    # Test for the presence of the DHCP service on DestinationServer
    if (-not (Get-Service dhcpserver -ComputerName $DestinationServer -ErrorAction SilentlyContinue)) {
      Write-Error "The DHCP server service is not installed on $DestinationServer" -Category InvalidOperation
      break
    }

    Write-Verbose "Importing DHCP server configuration into $DestinationServer"
    netsh dhcp server "\\$DestinationServer" import "c:\$ExportPath\dhcpserver.txt" all

  } else {
    Write-Error "Failed to locate DHCP configuration export file" -Category InvalidOperation
  }
}

Open in new window

If the usage is unclear please let me know. All you should need to do is paste the main block in (as you've been doing), then run:
Copy-DhcpServer OldServer NewServer

Open in new window

Chris
0
 
LVL 15

Author Comment

by:cwstad2
ID: 39836881
Hi Chris, i think im missing something here i keep getting error. Im not sire where to enter the server names.  win2k3 server is named serv2003 and the destination server is serv2008.

thanks
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 39837122
Can you share details of the error? I can't test the code myself, no DHCP servers to run it against so if it's going wrong detail will help a lot.

In theory, if you paste in the main block of code above; the Copy-DhcpServer function you should be able to run:
Copy-DhcpServer serv2003 serv2008

Open in new window

Chris
0
 
LVL 15

Author Closing Comment

by:cwstad2
ID: 39845328
thanks Chris much appreciated
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question