PowerShell, Technology

Managing DHCP Scopes with PowerShell

Contents

Introduction

We recently restructured our whole IP addressing schema. This was a mammoth job. The management of DHCP scopes was one part of this. We had to create around 20 new DHCP scopes and create reservations for all the devices that reservations in the old scopes.
I thought to myself that this is the perfect time to learn about using Powershell to manage DHCP scopes. Similar to when I started to learn Powershell to manage other areas of, it’s easier than I thought.

Creating DHCP scopes

You can create the DHCP scopes using an import from a CSV. You need to create this CSV with the information you want to import. The headings we used in our CSV are:

ScopeID         Name        StartRange       EndRange           SubnetMask        Router
172.25.70.0    House1     172.25.70.20    172.25.71.250     255.255.254.0    172.25.71.254
172.25.80.0    House2     172.25.80.20    172.25.81.250     255.255.254.0    172.25.81.254

Import the DHCP scope CSV

$dhcpScopeSource = Import-Csv “C:\FolderPath\DHCP-Import.csv"

Create the DHCP Scopes

The following lines of code loops through each scope in your CSV and does the following:
– Create the DHCP scope with the information you have imported from the CSV
– Set the router (gateway) for this newly created scope using the router field in the CSV

ForEach($dhcpScope In $dhcpScopeSource){
Add-DhcpServerv4Scope -Name $dhcpScope.Name -SubnetMask $dhcpScope.SubnetMask -StartRange $dhcpScope.StartRange -EndRange $dhcpScope.EndRange 
Set-DhcpServerv4OptionValue -ScopeId $dhcpScope.ScopeID -Router $dhcpScope.Router
}

Creating DHCP Reservations

You first need to build up a CSV of the reservations you want to import. The columns and format we used for our CSV was:

IPAddress         ScopeId         Mac                      Name
172.25.7.101    172.25.7.0     0898e1064076     RC1
172.25.7.102    172.25.7.0     0898e1023373     RC2

Import the CSV

$reservationsToAdd = Import-Csv "C:\FolderPath\NEwReservationImport.csv"

Create Reservations

The following lines of code loops through the imported CSV and creates the reservations using the information in the CSV

ForEach($device in $reservationsToAdd)
{
Add-DhcpServerv4Reservation -ScopeId $device.ScopeId -IPAddress $device.IPAddress -ClientId $device.Mac -Name $device.Name
}

 

 

Leave a Reply

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