PowerShell DhcpServer module (1/5)
The DhcpServer PowerShell module manages a Windows DHCP server: authorization, scopes, exclusions, reservations, classes, policies and failover. This part covers the common parameters and the first Add- cmdlets, with dnsmasq in Docker as the hands-on equivalent (the module is not installed on the office PC: Count 0).
246.1 Where it runs and common parameters
- Comes with the DHCP Server role of Windows Server or the RSAT DHCP Server Tools.
- Syntax is from Microsoft Learn; the lab uses dnsmasq (server1 192.168.246.10, server2 .11) and a udhcpc client.
Get-Command -Module DhcpServer ` -ErrorAction SilentlyContinue | Measure-ObjectPS C:\> Get-Command -Module DhcpServer -ErrorAction SilentlyContinue | Measure-Object Count : 0| Parameter | What it does |
|---|---|
| -ComputerName server01 | Target a remote DHCP server |
| -PassThru | Return the new object |
| -AsJob | Run as a background job |
| -CimSession Server01 | Run on a remote CIM session |
| -Confirm / -WhatIf | Ask first / only show what would happen |
| -ThrottleLimit 8 | Max parallel operations |
246.2 How DHCP works (DORA)
- Step 1
1.Laptop joins the LAN and broadcasts DHCPDISCOVER
- Step 2
2.DHCP1 offers a free IP from its scope: OFFER .120
- Step 3
3.Laptop sends REQUEST .120
- Step 4
4.Server replies ACK with a 1h lease + options (router, DNS, domain)
- Step 5
5.Exclusion range IPs are never offered
- Step 6
6.A reservation always gives the same IP to one MAC
246.3 Authorize the server and create a scope
Add-DhcpServerInDC ` -DnsName "dhcpserver.contoso.com" ` -IPAddress 10.10.10.2 Add-DhcpServerSecurityGroup ` -ComputerName "DhcpServer03.Contoso.com" Add-DhcpServerv4Scope -Name "Lab-4 Network" ` -StartRange 10.10.10.1 ` -EndRange 10.10.10.254 ` -SubnetMask 255.255.255.0 Add-DhcpServerv4Superscope ` -SuperscopeName "Expanded Lab-4 Network" ` -ScopeId 10.1.1.0, 10.1.2.0- In a domain a DHCP server leases only after it is authorized in AD (Add-DhcpServerInDC).
- Only -DnsName: IP comes from a DNS lookup; neither: the local server is added.
- Add-DhcpServerSecurityGroup creates DHCP Users + DHCP Administrators.
- Scope: -Name, -StartRange, -EndRange, -SubnetMask required; -LeaseDuration 8.00:00:00; -State Active|InActive; -Type Dhcp|Bootp|Both; -Delay (ms).
- Superscope groups several scopes; -Force moves a scope out of its old superscope.
246.4 Exclusion vs reservation
Add-Dhcpserverv4ExclusionRange ` -ScopeId 10.1.1.0 ` -StartRange 10.1.1.1 -EndRange 10.1.1.10 Add-DhcpServerv4Reservation ` -ScopeId 10.10.10.0 -IPAddress 10.10.10.8 ` -ClientId "F0-DE-F1-7A-00-5E" ` -Description "Reservation for Printer" Import-Csv -Path "Reservations.csv" | Add-DhcpServerv4Reservation| Point | Exclusion range | Reservation |
|---|---|---|
| Purpose | IPs that are never leased | One fixed IP for one MAC (ClientId) |
| Cmdlet | Add-DhcpServerv4ExclusionRange | Add-DhcpServerv4Reservation |
| Single IP | StartRange = EndRange | -IPAddress |
| Together | Wins over normal leasing | The only exception to an exclusion |
| dnsmasq | Split dhcp-range lines (gap) | dhcp-hostsfile: MAC,IP,name |
interface=eth1port=0dhcp-range=tag:!printer,192.168.246.100, 192.168.246.149,255.255.255.0,1hdhcp-range=tag:!printer,192.168.246.161, 192.168.246.200,255.255.255.0,1hdhcp-option=option:routerdhcp-option=option:dns-server,192.168.246.10dhcp-option=option:domain-name,branch.labdhcp-authoritativedhcp-leasefile=/tmp/dnsmasq.leases$ grep DHCP /tmp/dnsmasq.log (server1)dnsmasq-dhcp[37]: 2662181647 DHCPDISCOVER(eth1) 192.168.246.155 16:bf:3f:13:c2:20dnsmasq-dhcp[37]: 2662181647 DHCPOFFER(eth1) 192.168.246.107 16:bf:3f:13:c2:20dnsmasq-dhcp[37]: 2662181647 DHCPACK(eth1) 192.168.246.107 16:bf:3f:13:c2:20- The client asked for the excluded .155 but got .107: the gap .150-.160 acts as an exclusion.
- Then a reservation for the client MAC (hosts file + pkill -HUP dnsmasq) gave exactly .155.
$ cat /etc/n246-hosts (server1)16:bf:3f:13:c2:20,192.168.246.155,printer01$ udhcpc -i eth0 -n -q -R -t 3 -T 2 -s /tmp/hook.shudhcpc: lease of 192.168.246.155 obtained from 192.168.246.10, lease time 3600$ grep DHCPACK /tmp/dnsmasq.log (server1)dnsmasq-dhcp[37]: 3743217158 DHCPACK(eth1) 192.168.246.155 16:bf:3f:13:c2:20 printer01246.5 Leases, filters and multicast scopes
- Add-DhcpServerv4Lease: adds a lease record directly; docs say testing only. -IPAddress, -ScopeId, -ClientId required.
- Add-DhcpServerv4Filter -List Allow|Deny -MacAddress: MAC allow/deny list; Import-Csv for many MACs.
- Multicast scope: leases 224.x addresses (e.g. audio conference); -Ttl 20 = crosses 20 routers; default lease 30 days; -ExpiryTime infinite by default.
- Add-DhcpServerv4MulticastExclusionRange blocks some addresses inside the multicast scope.
Add-DhcpServerv4Filter -List Allow ` -MacAddress "F0-DE-F1-7A-00-5E" ` -Description "Laptop 09" Add-DhcpServerv4MulticastScope ` -ComputerName "DhcpServer01.Contoso.com" ` -Name "Multicast_AudioConference" ` -StartRange 224.0.0.0 -EndRange 224.0.0.30 ` -State Inactive -Ttl 20246.6 Classes, option definitions and policies
- Step 1
1.Printer sends vendor class LabPrinter (option 60)
- Step 2
2.Policy matches the class -> printer range -> 192.168.246.47
- Step 3
3.Lab laptop sends user class LabComputers (option 77)
- Step 4
4.Policy gives domain name labs.branch.lab
- Step 5
5.Any other client -> normal range and branch.lab
Add-DhcpServerv4Class -Name "MSUCClient" ` -Type Vendor -Data "MS-UC-Client"Add-DhcpServerv4Class ` -Name "User Class for Lab Computers" ` -Type User -Data "LabComputers"Add-DhcpServerv6Class ` -Name "Vendor Class for Printers" ` -Type "Vendor" -Data "JetPrinters" ` -VendorId 100 Add-DhcpServerv4OptionDefinition ` -Name "WPAD" -OptionId 252 -Type "String" Add-DhcpServerv4Policy -Name "PrinterPolicy" ` -ScopeId 10.10.10.0 -Condition OR ` -VendorClass EQ,"HP Printer","Xerox Printer"Add-DhcpServerv4PolicyIPRange ` -ScopeId 10.10.10.0 -Name "HyperVPolicy" ` -StartRange 10.10.10.10 ` -EndRange 10.10.10.20- Class: -Type Vendor|User and -Data = the value the client sends; v6 vendor classes also need -VendorId (enterprise number).
- Option definition first, value later: -OptionId 1-255, -Type Byte|Word|DWord|IPv4Address|String|BinaryData...; -VendorClass limits it to one class.
- Policy conditions (EQ/NE, * wildcard): -VendorClass, -UserClass, -MacAddress, -ClientId, -Fqdn, -RelayAgent, -CircuitId, -RemoteId, -SubscriberId.
- -Condition And|Or, -ProcessingOrder, -Enabled $False, -LeaseDuration; with -ScopeId = scope level, without = server level.
dhcp-vendorclass=set:printer,LabPrinterdhcp-range=tag:printer,192.168.246.40, 192.168.246.49,255.255.255.0,1hdhcp-userclass=set:lab,LabComputersdhcp-option=tag:lab,option:domain-name, labs.branch.lab # client (udhcpc):udhcpc -V LabPrinter ...udhcpc -x 0x4d:4c6162436f6d707574657273 (option 77 = "LabComputers" in hex)udhcpc -V SomethingElse ...$ grep -E 'class|tags' /tmp/dnsmasq.log (server1)dnsmasq-dhcp[37]: 3999465991 vendor class: LabPrinterdnsmasq-dhcp[37]: 3999465991 tags: printer, eth1dnsmasq-dhcp[37]: 750437434 user class: LabComputersdnsmasq-dhcp[37]: 750437434 tags: lab, eth1dnsmasq-dhcp[37]: 3688401949 vendor class: SomethingElse246.7 Failover
- Step 1
1.DHCP1 and DHCP2 share the same scope and sync leases
- Step 2
2.Load balance: DHCP1 serves 70% of clients, DHCP2 30%
- Step 3
3.DHCP1 goes down
- Step 4
4.DHCP2 enters COMMUNICATION INTERRUPTED
- Step 5
5.After StateSwitchInterval -> PARTNER DOWN: DHCP2 serves the whole scope
- Step 6
6.Clients keep the same lease because it was synced
Add-DhcpServerv4Failover ` -ComputerName "dhcpserver.contoso.com" ` -Name "SFO-SIN-Failover" ` -PartnerServer "dhcpserver2.contoso.com" ` -ScopeId 10.10.10.0,10.20.20.0 ` -LoadBalancePercent 70 ` -MaxClientLeadTime 2:00:00 ` -AutoStateTransition $True ` -StateSwitchInterval 2:00:00 Add-DhcpServerv4FailoverScope ` -Name "SFO-SIN-Failover" ` -ScopeId 10.10.10.0,10.20.20.0| Mode | Parameters | Behaviour |
|---|---|---|
| Load balance (active-active) | -LoadBalancePercent 70 | Both serve clients in a split |
| Hot standby | -ServerRole Active|Standby, -ReservePercent 10 | Standby serves only when active fails |
| Both | -AutoStateTransition, -StateSwitchInterval, -SharedSecret, -MaxClientLeadTime | Partner-down timer, message authentication |
- Scopes listed in -ScopeId are created on the partner automatically.
- Lab (NOT real failover): server2 runs a separate range .201-.220 (split scope). With server1 stopped the client got .208, a NEW IP, because no leases are synced.
$ grep DHCP /tmp/dnsmasq.log (server2)dnsmasq-dhcp[40]: 736626814 DHCPOFFER(eth1) 192.168.246.208 16:bf:3f:13:c2:20dnsmasq-dhcp[40]: 736626814 DHCPACK(eth1) 192.168.246.208 16:bf:3f:13:c2:20246.8 Common mistakes
- Thinking two DHCP servers on a subnet = failover: a split scope has no lease sync, so clients change IP.
- Forgetting to authorize the server in AD: it will not lease in a domain.
- Setting an option value before its option definition exists.
- Using Add-DhcpServerv4Lease in production: docs say it is for testing only.
246.9 Interview tips
- Separate printer range: Add-DhcpServerv4Class -Type Vendor -> Add-DhcpServerv4Policy -VendorClass EQ,... -> Add-DhcpServerv4PolicyIPRange.
- Range for nobody but one printer needs .155: exclusion range + reservation (reservation is the exception).
- Failover modes: load balance (e.g. 70/30) vs hot standby (ReservePercent); states COMMUNICATION INTERRUPTED -> PARTNER DOWN.
- DORA = Discover, Offer, Request, Ack.