This might take you 2 minutes to read.

I got a task to create 142+ dhcp scopes. Reason? Migrating from a local dhcp on every site to a central dhcp server. I had to use the same scopes that was on each site.

My resolution was to create a csv file with all settings and create a powershell script.

All networks was all the same. Same setup: gateway on 254, printers up till 31. all printed in the csv.
The csv had following layout, with “;” in between.

Name begin end Gateway Submask
locationName1 10.10.1.32 10.10.1.253 10.10.1.254 255.255.255.0
locationName2 10.10.35.32 10.10.35.253 10.10.35.254 255.255.255.0

Save it as d:tempimportdhcp.csv and use the script below.

$path = "d:tempimportdhcp.csv"
$dhcpscopes = import-csv $path -Delimiter ";"
$dhcpCount = (Get-Content $path).count -1
$i = 0
foreach($dhcpscope in $dhcpscopes){
$i += 1
write-progress -activity "Creating DHCPscopes" -status $dhcpscope.name -percentcomplete ($i/$dhcpCount*100)
Add-DhcpServerv4Scope -Name $dhcpscope.name -StartRange $dhcpscope.begin -EndRange $dhcpscope.end -SubnetMask $dhcpscope.submask
Set-DhcpServerv4OptionValue -ScopeId (($dhcpscope.begin).Substring(0,($dhcpscope.begin.Length)-2) + 0) -Router $dhcpscope.gateway

}

 

Ok. So now I was done. If you are like me you forgot something. In my case I forgot to see the lease time to 4 hours. so what i did was to get all dhcp-scopes and piped them into to set-dchp-scope and set lease time. Get-DhcpServerv4Scope | Set-DhcpServerv4Scope -LeaseDuration 0.04:00:00

 

after that I realised that the end range was wrong so I needed to correct that one also. i had but 250 as a end ip and it should have been 240 so i needed to take all dhcp scopes and parse through and set begin and end range for each scope.

$scopes = get-dhcpserverv4scope
foreach ($scope in $scopes) {
$baseip = ($scope.ScopeId.IPAddressToString).Substring(0,($scope.ScopeId.IPAddressToString).Length - 2)
$begin = $baseip + ".32"
$end = $baseip + ".240"
set-dhcpserverv4scope -scopeid $scope.ScopeId.IPAddressToString -startrange $begin -endrange $end

}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.