Wednesday, July 16, 2014

DCHP server migration from Debian to Windows Server 2008

My customer wanted to migrate DHCP server function from Debian Wheezy 7.50 running ISC-DHCP to Windows Server 2008.
The task can broken in 6 parts

  1. parse dhcpd.conf on the DHCP server (Linux)
  2. import the parsed file to your *new* DHCP server
  3. setup DHCP role on *new*server
  4. run a script on the *new* DHCP server which creates scopes,pools and reservations (Windows) according to the parsed file.
  5. manually rename the scopes to "friendly" names
  6. manually set the server,scope and/or reservation options and scope lease times
Parse dhcpd.conf
get this great AWK parser onto the Debian box https://gist.github.com/mattpascoe/4039747
make it executable
chmod +x dhcpparse.awk
parse the file
cat /etc/dhcp/dhcpd.conf | dhcpparse.awk > dhcp-config.txt

Import the parsed file to your Windows Server
have a look at it to remove errors.
Setup DHCP role on Windows Server
skipping this part as it's pretty self explanatory
Run script
You will need this Powershell module , referred to as "Microsoft.DHCP.Powershell.Admin.psm1" in the script. If Window Server is version 2012 R2 , I guess you can use the DHCP server cmdlets from Microsoft instead.
You will also need to make a Powershell module -which is a combination of this function and this function. Just add one function under the other and put the line "export-modulemember IsIpAddressInRange,Get-IPrange". This module is referred as "IPutil.psm1"

Import-Module .\Microsoft.DHCP.Powershell.Admin.psm1

Import-Module .\IPutil.psm1



#Group by line types

$subnets = Select-String -Pattern 'subnet' -Path .\dhcp-config.txt |%{$_.Line}

$pools = Select-String -Pattern 'pool' -Path .\dhcp-config.txt | %{$_.Line}

$hosts = Select-string -Pattern ‘host’ -path  .\dhcp-config.txt |%{$_.Line}



#Create scopes

$scopes = $subnets |%{$l=$_.Split(','); New-DHCPScope -Server $env:COMPUTERNAME -Address $l[1] -SubnetMask $l[2] -Name $l[3]}





foreach ($scope in $scopes)

{

    # generate all the IP addresses in this scope

    $ips=Get-IPrange -ip $scope.Address -mask $scope.SubnetMask

    # Create pools

    Write-host "Creating pool(s) for " $scope.Address

    foreach ($line in $pools)

    {

        if($ips -contains $line.split(',')[1])

        {

            Add-DhcpIPRange -scope $scope -startaddress $line.split(',')[1] -endaddress $line.split(',')[2]

        }

    }

    # Create reservations

    Write-host "Creating reservation(s) for " $scope.Address

    foreach ($line in $hosts)

    {

        if($ips -contains $line.split(',')[1])

        {

            New-DHCPReservation -scope $scope -IPAddress $line.split(',')[1] -MACAddress $line.split(',')[2] -Description $line.split(',')[4]

        }

    }

}

Write-host “You should now rename the scopes to friendly names”

Write-host “You should manually set options and lease times"

2 comments:

  1. Hi, can you help to understand what is happen ?! I made all steps above, after I launch the final script, receive the following errors:
    New-DHCPScope : Cannot validate argument on parameter 'Address'. The argument "" does not match the "\b(?:(?:25[0-5]|2[
    0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b" pattern. Supply an argument that matches "
    \b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b" and try the command agai
    n.
    At C:\DHCP-Conf.ps1:16 char:89
    + $scopes = $subnets |%{$l=$_.Split(','); New-DHCPScope -Server $env:COMPUTERNAME -Address <<<< $l[1] -SubnetMask $l[2
    ] -Name $l[3]}
    + CategoryInfo : InvalidData: (:) [New-DHCPScope], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,New-DHCPScope

    New-DHCPScope : Cannot validate argument on parameter 'Address'. The argument "" does not match the "\b(?:(?:25[0-5]|2[
    0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b" pattern. Supply an argument that matches "
    \b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b" and try the command agai
    n.
    At C:\DHCP-Conf.ps1:16 char:89
    + $scopes = $subnets |%{$l=$_.Split(','); New-DHCPScope -Server $env:COMPUTERNAME -Address <<<< $l[1] -SubnetMask $l[2
    ] -Name $l[3]}
    + CategoryInfo : InvalidData: (:) [New-DHCPScope], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,New-DHCPScope

    ReplyDelete