Need to mix the convenience of DHCP with the consistency of a static IP? Use a DHCP reservation, and create them with PowerShell.
DHCP reservations explained
DHCP empowers us to automatically assign IP addresses to clients while adhering to standardized rules (address space, lease duration, DNS servers, etc). For most clients, we typically don’t care what IP address the client ends up with on any given day, but for other devices consistently connected to the network, there are benefits to having the device get the same IP every time. A prime example that everyone can relate to is a printer. If you have the printer shared through a print server, the print server needs to be able to consistently talk to the printer on a known IP address.
“A DHCP reservation is a permanent IP address assignment. It is a specific IP address within a DHCP scope that is permanently reserved for leased use to a specific DHCP client” (tech-faq.com).
In the previous post we created DHCP scopes. Let’s see how we can use PowerShell to create a DHCP reservation within one of those scopes.
Creating a DHCP reservation using PowerShell
For this demo, we will reference a Brother HL-2270DW printer as the device for which we want to create a DHCP reservation. In order to create the reservation, we need the MAC address of the printer. Note that a device may have more than one MAC address, you will want to identify the MAC address for the interface you are connecting (wired or wireless).
The MAC address for my printer’s wired ethernet interface is 30055c077312. Given this information, I can use the PowerShell cmdlet Add-DhcpServerv4Reservation:
# Create DHCP reservation $HashArgs = @{ 'ComputerName' = 'DhcpDemo'; 'ScopeId' = '192.168.2.0'; 'ClientId' = '30055c077312'; 'Name' = 'Brother Printer'; 'IPAddress' = '192.168.2.15'; } Add-DhcpServerv4Reservation @HashArgs
You can see that the reservation was successfully created using Get-DhcpServerv4Reservation or by using the MMC snap-in.
PS C:\Windows\system32> Get-DhcpServerv4Reservation -ComputerName DhcpDemo -ScopeId 192.168.2.0 IPAddress ScopeId ClientId Name Type Description --------- ------- -------- ---- ---- ----------- 192.168.2.15 192.168.2.0 30-05-5c-07-73-12 Brother Printer Both
Copy DHCP reservations from an existing Windows DHCP Server
If you are already using a Windows DHCP server in your environment but are migrating to a new DHCP server, you can use PowerShell to copy the configured DHCP reservations from your old server to your new server. This will eliminate a lot of manual reservation creation or per-reservation scripting. The following example assumes you a have already created matching scopes on the New DHCP server from the old DHCP server:
# Copy DHCP reservations from one DHCP server to another Get-DhcpServerv4Scope -ComputerName OldDhcpServer | Get-DhcpServerv4Reservation | Add-DhcpServerv4Reservation -ComputerName NewDhcpServer -Whatif
Next Steps: Use PowerShell DSC resources xDHCPServer
Up to this point, we have used interactive PowerShell cmdlets to install and configure our DHCP server. While this is certainly a legitimate approach, in my next post we will configure the same server using a DSC configuration.
Francois Cleroux says
We have 3 DHCP Servers, how do I specify which server to make the Reservation on? I need to do it on all 3 servers.
Aaron Rothstein says
I think the best approach would be to create a CimSession for the DHCP server you want to run your commands against, then specify the session object using the -CimSession parameter.