The task can broken in 6 parts
- parse dhcpd.conf on the DHCP server (Linux)
- import the parsed file to your *new* DHCP server
- setup DHCP role on *new*server
- run a script on the *new* DHCP server which creates scopes,pools and reservations (Windows) according to the parsed file.
- manually rename the scopes to "friendly" names
- manually set the server,scope and/or reservation options and scope lease times
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"
Nice and informative post. I appreciate your hard work.
ReplyDeletewindows 8 to 10 migration
windows 10 packaging
Hi, can you help to understand what is happen ?! I made all steps above, after I launch the final script, receive the following errors:
ReplyDeleteNew-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