Export DHCP from Server 2003 to 2008

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.
LVL 15
cwstad2Asked:
Who is Participating?
 
Chris DentConnect With a Mentor PowerShell DeveloperCommented:
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
 
cwstad2Author Commented:
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
 
Chris DentPowerShell DeveloperCommented:
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
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
cwstad2Author Commented:
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
 
Chris DentPowerShell DeveloperCommented:
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
 
cwstad2Author Commented:
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
 
Chris DentPowerShell DeveloperCommented:
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
 
cwstad2Author Commented:
thanks Chris much appreciated
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.