How to import DHCP reservations from a CSV file using PowerShell

DHCP reservations are particularly useful to avoid assigning fixed IP addresses to your network devices. It is common practice to add reservations to the DHCP database before the operating system is installed. You can then boot up the computer via PXE to deploy the correct start image. This way, you can also automatically assign the host name to the server.

Now, you can automate the task by importing a CSV file with the help of PowerShell if you have to add many DHCP reservations. You may also set values for options with this procedure. In this guide, we'll walk you through the steps to create a CSV file and import it using PowerShell adding DHCP reservations in bulk on any Windows DHCP Server.


Preparing the CSV file

Creating many DHCP reservations via PowerShell requires several steps. First, you have to build a CSV file that contains all the necessary information such as the scope that contains the corresponding reservations, the name, and the client’s MAC address.

After importing the data records from the CSV file with Import-Csv and passing them through a pipe to Add-DhcpServerv4Reservation, the field names have to exactly match the parameter names that the cmdlet accepts. For example, the column with the MAC addresses corresponds to ClientId or the scope with ScopeId (by default PowerShell is case-insensitive).


Finding available IP addresses

Of course, you could include the IP addresses for each entry in the CSV file and pass them in the same way as the other information to Add-DhcpServerv4Reservation. However, you then had to read the available addresses from the leases risking that some of them are already assigned to other devices before you import the data form the CSV file.

Because of this, I recommend retrieving an available IP address just before making the reservation. You can get the address with the help of Get-DhcpServerv4FreeIPAddress and pass it to Add-DhcpServerv4Reservation through the parameter IPAddress.


Working with scope options

You will probably also want to assign certain scope options such as the host name, the domain, or the server name for a PXE boot. You cannot set these options via PowerShell when adding the addresses. Instead you have to call Set-DhcpServerv4OptionValue for this purpose after making the reservation.

DHCP options have numbers which you can get from the DHCP management snap-in.

Getting scope options from the DHC snap-in

The sample code below reads the options 66 (Boot Server Host Name) and 67 (Bootfile Name) from the CSV file. The file contents also reach the call of Set-DhcpServerv4OptionValue through the pipe. 

function bulkDHCPReservations{


   Import-Csv .\DHCP-Reservierungen.csv | foreach{
     $FreeIP = Get-DhcpServerv4FreeIPAddress -Scopeid $_.ScopeId `
               -ComputerName $DHCPSrv
     $_ | Add-DhcpServerv4Reservation -IpAddress $FreeIp -ComputerName $DHCPSrv

     Set-DhcpServerv4OptionValue -optionID 66 -value $_.Option66 `
        -ReservedIP $FreeIP -ComputerName $DHCPSrv
     Set-DhcpServerv4OptionValue -optionID 67 -value $_.Option67 `
        -ReservedIP $FreeIP -ComputerName $DHCPSrv

The function bulkDHCPReservations expects the name of the DHCP server where you want to create the reservations as the only parameter.


No comments:

Powered by Blogger.