PowerShell DhcpServer module (3/5)
Part 3 of the DhcpServer module: scope and server statistics, superscopes, the DHCPv6 Get- cmdlets, Import-DhcpServer with failover replication, and the first Remove- cmdlets. The dnsmasq lab validates a config, runs DORA for three clients and computes Free/InUse/PercentageInUse from the lease file (the module is not installed on the office PC: Count 0).
248.1 Why is it used?
- When new laptops get no IP, the usual cause is a full scope: statistics show it before users complain.
- Import + replication move or keep DHCP config in sync across servers.
- Remove- cmdlets clean up leases, exclusions, classes and AD authorization, but some delete far more than you expect.
248.2 How scope statistics work
- Step 1
1.Scope has only 10 addresses
- Step 2
2.pc1 broadcasts DHCPDISCOVER -> Offer + Ack 192.168.10.107 (1h lease)
- Step 3
3.pc2 -> Ack 192.168.10.108: 2 addresses in use
- Step 4
4.Admin PC runs Get-DhcpServerv4ScopeStatistics
- Step 5
5.Server returns Free 8, InUse 2, 20%
- Step 6
6.Read-only: nothing changes
248.3 IPv4 statistics and superscopes
Get-DhcpServerv4ScopeStatistics ` -ComputerName "dhcpServer.contoso.com"Get-DhcpServerv4ScopeStatistics | Where-Object -FilterScript { $_.PercentageInUse -Gt 80 } Get-DhcpServerv4Statistics ` -ComputerName "dhcpserver.contoso.com" Get-DhcpServerv4SuperScope ` -ComputerName "dhcpserver.contoso.com"Get-DhcpServerv4SuperScopeStatistics ` -Name "SS1" ` -ComputerName "DhcpServer01.Contoso.com"- ScopeStatistics: no -ScopeId = all scopes; -Failover adds failover statistics.
- Get-DhcpServerv4Statistics: all IPv4 counters of the whole server.
- Superscope = several scopes on the same physical network; -SuperscopeName shows one, none shows all.
248.4 DHCPv6 Get- cmdlets
| Cmdlet | Notes |
|---|---|
| Get-DhcpServerv6Binding | IPv6 interfaces the server is bound to |
| Get-DhcpServerv6Scope / ScopeStatistics | -Prefix for one scope (instead of -ScopeId), none for all |
| Get-DhcpServerv6Statistics | Server-wide v6 counters |
| Get-DhcpServerv6StatelessStatistics / StatelessStore | Stateless clients take only options, no address |
| Get-DhcpServerv6Lease | -Prefix (scope) or -IPAddress (one lease) |
| Get-DhcpServerv6FreeIPAddress | -Prefix required; 1 by default, -NumAddress up to 1024 |
| Get-DhcpServerv6ExclusionRange / Reservation | Reservations show DUID + IAID |
| Get-DhcpServerv6OptionValue | Server / -Prefix / -ReservedIP; -All, -Brief |
| Get-DhcpServerv6DnsSetting | Effective setting (inherited from the level above if empty) |
Get-DhcpServerv6FreeIPAddress ` -Prefix 2001:4898:7020:1020:: -NumAddress 10Get-DhcpServerv6Scope | Get-DhcpServerv6Reservation248.5 Import-DhcpServer and failover replication
- Step 1
1.Export-DhcpServer (previous part) produced dhcpexport.xml from dhcp1
- Step 2
2.Admin PC runs Import-DhcpServer on dhcp2
- Step 3
3.dhcp2 first backs up its database to BackupPath (mandatory)
- Step 4
4.Scopes (and leases with -Leases) are imported
- Step 5
5.Invoke-DhcpServerv4FailoverReplication copies scope config, reservations, options, policies to the partner
Import-DhcpServer ` -ComputerName "dhcpserver.contoso.com" ` -File "C:\exports\dhcpexport.xml" ` -BackupPath "C:\dhcpbackup\" $ScopeIdList = Import-Csv ` -Path ".\ScopeList.txt"# -ScopeId $ScopeIdList.ScopeId Invoke-DhcpServerv4FailoverReplication ` -ComputerName "dhcpserver.contoso.com" ` -Name "SFO-SIN-Failover"- -File and -BackupPath are both required; -Leases imports leases too; -ScopeId / -Prefix limit the scopes.
- Existing scope without -ScopeOverwrite: warning, import moves to the next scope.
- -ServerConfigOnly imports only server-level settings.
- Replication: -Name = one relationship, -ScopeId = those scopes, nothing = all failover scopes.
- Remove-DhcpServerv4Failover removes the relationship from BOTH servers; Remove-DhcpServerv4FailoverScope deletes the scopes from the partner only.
248.6 Remove- cmdlets
Remove-DhcpServerInDC ` -DnsName "dhcpserver.contoso.com" ` -IPAddress 10.10.10.2Remove-DhcpServerDnsCredential ` -ComputerName "DhcpServer03.Contoso.com" Get-DhcpServerv4Lease -ScopeId 10.10.10.0 ` -AllLeases | Remove-DhcpServerv4LeaseRemove-DhcpServerv4ExclusionRange ` -ScopeId 10.10.10.0 ` -StartRange 10.10.10.1 -EndRange 10.10.10.10Remove-DhcpServerv4Class ` -Name "MSUCClient" -Type VendorRemove-DhcpServerv4Filter ` -MacAddress "F0-DE-F1-7A-00-5E"(docs) server AD me nahi ho to:The DHCP server is already de-authorized in Active Directory.The authorization check on the DHCP server has been initiated.| Cmdlet + parameters | What gets deleted |
|---|---|
| Remove-DhcpServerv4Lease -ScopeId only | ALL leases of the scope |
| Remove-DhcpServerv4Lease -IPAddress / -ClientId | Only those clients' leases |
| Remove-DhcpServerv4Lease -ScopeId -BadLeases | Only declined leases (without ScopeId: all scopes) |
| Remove-DhcpServerv4ExclusionRange without Start/End | ALL exclusion ranges of the scope |
| Remove-DhcpServerv4MulticastLease without -IPAddress | All leases of that multicast scope |
| Remove-DhcpServerInDC | De-authorizes the server in AD (only -IPAddress = error) |
- After Remove-DhcpServerDnsCredential the server uses its own system account for DNS registration.
- Removing a multicast exclusion range makes those addresses available immediately.
248.7 Lab: config test, DORA, lease file
dnsmasq --test -C /etc/n248-dnsmasq.confdnsmasq --test -C /etc/n248-broken.confudhcpc -i eth0 -n -q -t 3 \ -x hostname:pc1 -s /tmp/hook.sh$ dnsmasq --test -C /etc/n248-dnsmasq.confdnsmasq: syntax check OK.$ grep '^dhcp-range' /etc/n248-broken.confdhcp-range=192.168.248.100,192.168.248.300,255.255.255.0,1h$ dnsmasq --test -C /etc/n248-broken.confdnsmasq: bad dhcp-range at line 5 of /etc/n248-broken.conf(exit 1)dnsmasq-dhcp[75]: 2639589413 DHCPDISCOVER(eth1) 02:48:00:00:00:01dnsmasq-dhcp[75]: 2639589413 DHCPOFFER(eth1) 192.168.248.107 02:48:00:00:00:01dnsmasq-dhcp[75]: 2639589413 DHCPDISCOVER(eth1) 02:48:00:00:00:01dnsmasq-dhcp[75]: 2639589413 DHCPOFFER(eth1) 192.168.248.107 02:48:00:00:00:01dnsmasq-dhcp[75]: 2639589413 DHCPREQUEST(eth1) 192.168.248.107 02:48:00:00:00:01dnsmasq-dhcp[75]: 2639589413 DHCPACK(eth1) 192.168.248.107 02:48:00:00:00:01 pc1- Scope .100-.109 (10 addresses); pc1 got .107, pc2 .108, pc3 .109, lease time 3600.
- DISCOVER appears twice because udhcpc broadcast it two times.
$ cat /tmp/dnsmasq.leases1794253644 02:48:00:00:00:03 192.168.248.109 pc3 01:02:48:00:00:00:031794253633 02:48:00:00:00:02 192.168.248.108 pc2 01:02:48:00:00:00:021794253620 02:48:00:00:00:01 192.168.248.107 pc1 01:02:48:00:00:00:01192.168.248.109 02:48:00:00:00:03 pc3 expires 19:47:24 UTC192.168.248.108 02:48:00:00:00:02 pc2 expires 19:47:13 UTC192.168.248.107 02:48:00:00:00:01 pc1 expires 19:47:00 UTC248.8 Lab: building scope statistics
awk -F'[=,]' 'FNR==NR { if ($1=="dhcp-range") { split($2,a,"."); split($3,b,".") t=b[4]-a[4]+1 }; next }{ u++ }END { printf "Free %d InUse %d ", t-u, u printf "PercentageInUse %d\n", u*100/t }' /etc/n248-dnsmasq.conf /tmp/dnsmasq.leases grep -oE 'DHCP[A-Z]+' /tmp/dnsmasq.log | sort | uniq -c$ awk ... (scope statistics, before the clients)Free 10 InUse 0 PercentageInUse 0$ awk ... (after pc1, pc2, pc3)Free 7 InUse 3 PercentageInUse 30$ grep -oE 'DHCP[A-Z]+' /tmp/dnsmasq.log | sort | uniq -c 3 DHCPACK 6 DHCPDISCOVER 6 DHCPOFFER 3 DHCPREQUEST- Total comes from dhcp-range, InUse from lease-file lines: the same idea as Get-DhcpServerv4ScopeStatistics; the message counts are the idea of Get-DhcpServerv4Statistics.
248.9 Common mistakes
- Giving a Remove- cmdlet only -ScopeId and wiping all leases or exclusions of the scope.
- Running Import-DhcpServer without -ScopeOverwrite and missing the warning that existing scopes were skipped.
- Using -ScopeId with v6 cmdlets: they take -Prefix.
- Starting dnsmasq without dnsmasq --test after editing the config.
248.10 Interview tips
- Find scopes about to fill: Get-DhcpServerv4ScopeStatistics | Where-Object PercentageInUse -Gt 80, alert on it.
- Then check Get-DhcpServerv4Lease for old/declined leases; add a superscope or new scope if needed.
- Import-DhcpServer always needs -BackupPath (it backs up the target first).
- Remove with only -ScopeId = everything in that scope: always Get- first, then a specific -IPAddress or range.