SkillPilotAI

PowerShell DhcpServer module (2/5)

Part 2 of the DhcpServer module: IPv6 (DHCPv6) scopes, exclusions, reservations and leases, Backup vs Export for migration, and the Get- cmdlets for server settings, scopes, leases and options. The lab uses dnsmasq for a reservation, a DHCPv6 lease, the lease file and a backup/restore (the module is not installed on the office PC: Count 0).

247.1 How DHCPv6 works

DHCPv6: Solicit, Advertise, Request, Reply
  1. 1.Laptop joins and sends Solicit to ff02::1:2 (all DHCPv6 servers)

  2. 2.DHCP1 sends Advertise from its IPv6 scope

  3. 3.Laptop sends Request

  4. 4.DHCP1 sends Reply: address + lease time

  5. 5.Client identity = DUID + IAID (not MAC as in IPv4)

One device can have many interfaces, so v6 reservations and leases need both ClientDuid and Iaid

247.2 IPv6 Add- cmdlets

PowerShell
Add-DhcpServerv6Scope `  -Prefix 2001:4898:7020:1020:: `  -Name "IPv6 Lab-4 Network" `  -PreferredLifeTime 4.00:00:00 `  -ValidLifeTime 6.00:00:00 -State Inactive Add-DhcpServerv6ExclusionRange `  -Prefix 2001:4898:7020:1020:: `  -StartRange 2001:4898:7020:1020::1 `  -EndRange 2001:4898:7020:1020::10 Add-DhcpServerv6Reservation `  -Prefix 2001:4898:7020:1020:: `  -IPAddress 2001:4898:7020:1020::1 `  -ClientDuid `  "00-01-00-01-15-F9-7F-AB-F0-DE-F1-7A-00-5E" `  -Iaid 234890455 Get-DhcpServerv6Lease -IPAddress `  2001:4898:7020:1020::11 |  Add-DhcpServerv6Reservation `  -Prefix 2001:4898:7020:1020::
  • v6 scope: -Prefix + -Name required; -PreferredLifeTime / -ValidLifeTime; -T1 renewal (default 4 days), -T2 rebind; -State Inactive = no leases yet.
  • v6 exclusion: -Prefix + -StartRange + -EndRange.
  • v6 reservation: -ClientDuid + -Iaid; Import-Csv for many; pipe Get-DhcpServerv6Lease to turn a running lease into a reservation.
  • Add-DhcpServerv6Lease: -IPAddress, -ClientDuid, -Iaid required; -AddressType IANA | IATA; -HostName, -LeaseExpiryTime.
  • Add-DhcpServerv6OptionDefinition -OptionId 36 -Type IPv6Address defines a new DHCPv6 option.

247.3 Backup vs Export: migration

Migrating DHCP1 to a new DHCP2
  1. 1.DHCP1 hardware is old; work must move to DHCP2

  2. 2.Backup-DhcpServer copies the database to dhcp\backup

  3. 3.Export-DhcpServer -Leases writes scopes, settings and leases to dhcpexport.xml

  4. 4.Import-DhcpServer on DHCP2 (next part)

  5. 5.Clients keep the same scopes and leases

PowerShell
Backup-DhcpServer `  -ComputerName "dhcpserver.contoso.com" `  -Path "C:\Windows\system32\dhcp\backup" Export-DhcpServer `  -ComputerName "dhcpserver.contoso.com" `  -File "C:\exportdir\dhcpexport.xml" `  -ScopeId 10.10.10.0,10.20.20.0 -Leases
PointBackup-DhcpServerExport-DhcpServer
OutputCopy of the database in a folderXML file of the configuration
Required-Path-File
LeasesPart of the databaseOnly with -Leases
Scope filterNo-ScopeId / -Prefix (server settings always exported)
Typical useRestore on the same serverMigration (Import-DhcpServer on the new server)

247.4 Server-level Get- cmdlets

CmdletShows
Get-DhcpServerInDCAuthorized DHCP servers in AD (no options)
Get-DhcpServerAuditLogAudit log settings
Get-DhcpServerDatabaseDatabase config parameters
Get-DhcpServerDnsCredentialAccount used to register client records in DNS
Get-DhcpServerSetting / Get-DhcpServerVersionService settings / major + minor version
Get-DhcpServerv4BindingIPv4 interfaces the server is bound to
  • Get-DhcpServerv4Class -Type Vendor|User; Get-DhcpServerv4DnsSetting at server, -ScopeId, -IPAddress (reservation) or -PolicyName level.
  • Get-DhcpServerv4Failover -Name / -ScopeId; Get-DhcpServerv4Filter -List Allow|Deny; Get-DhcpServerv4FilterList = are the lists enabled.

247.5 Scope, lease and option Get- cmdlets

PowerShell
Get-DhcpServerv4Scope |  Get-DhcpServerv4Reservation Get-DhcpServerv4FreeIPAddress `  -ScopeId 10.10.10.0 -NumAddress 10 Get-DhcpServerv4Lease -ScopeId 10.10.10.0 -AllLeasesGet-DhcpServerv4Scope |  Get-DhcpServerv4Lease Get-DhcpServerv4OptionValue -ScopeId 10.10.10.0Get-DhcpServerv4OptionValue -ReservedIP 10.10.10.5
  • Get-DhcpServerv4Scope / ExclusionRange: no -ScopeId = all.
  • Get-DhcpServerv4Reservation by -ScopeId, -ClientId or -IPAddress; pipe from Get-...Scope for all scopes.
  • FreeIPAddress skips exclusions, reservations and active leases; max 1024 addresses.
  • Get-DhcpServerv4Lease: active only by default; -AllLeases adds Offered/Declined/Expired; -BadLeases = declined only.
  • OptionValue: no params = server level; -ScopeId, -ReservedIP, -PolicyName, -VendorClass, -UserClass; -Brief skips names (faster).
  • Multicast: Get-DhcpServerv4MulticastScope / ScopeStatistics / ExclusionRange; MulticastLease needs -Name.

247.6 Lab: dnsmasq reservation, DHCPv6 and lease file

Output
interface=eth1port=0dhcp-range=192.168.247.100,192.168.247.150,  255.255.255.0,1hdhcp-host=02:47:00:00:00:50,  192.168.247.50,laptop1dhcp-range=::100,::1ff,constructor:eth1,  64,1hdhcp-leasefile=/tmp/dnsmasq.leases
  • dhcp-host = a reservation by MAC (here outside the pool); the second dhcp-range is a DHCPv6 scope on eth1's prefix.
  • netstat showed UDP 67 (DHCP) and UDP 547 (DHCPv6) listening.
Terminal
udhcpc -i eth0 -n -q -t 3 \  -x hostname:laptop1 -s /tmp/hook.shudhcpc6 -i eth0 -n -q -t 3 -s /tmp/hook6.shcat /tmp/dnsmasq.leases
Output
[client]$ udhcpc -i eth0 -n -q -t 3 -x hostname:laptop1 -s /tmp/hook.shudhcpc: started, v1.36.1udhcpc: broadcasting discoverudhcpc: broadcasting select for 192.168.247.50, server 192.168.247.10udhcpc: lease of 192.168.247.50 obtained from 192.168.247.10, lease time 3600[server1]$ grep DHCP /tmp/dnsmasq.logdnsmasq-dhcp[32]: 1571347 DHCPSOLICIT(eth1) 00:03:00:01:02:47:00:00:00:50dnsmasq-dhcp[32]: 1571347 DHCPADVERTISE(eth1) fd47:247::109 00:03:00:01:02:47:00:00:00:50dnsmasq-dhcp[32]: 1571347 DHCPREQUEST(eth1) 00:03:00:01:02:47:00:00:00:50dnsmasq-dhcp[32]: 1571347 DHCPREPLY(eth1) fd47:247::109 00:03:00:01:02:47:00:00:00:50
  • The lease file is the dnsmasq view of Get-DhcpServerv4Lease: expiry, MAC, IP, hostname for IPv4; IAID, address, DUID for IPv6; plus a duid line with the server's own DUID.

247.7 Lab: backup and restore to a new server

Terminal
docker exec n247-server1 cat \  /etc/n247-dnsmasq.conf > backup/dnsmasq.confdocker exec n247-server1 cat \  /tmp/dnsmasq.leases > backup/dnsmasq.leasespkill dnsmasqdocker exec -i n247-server2 sh -c \  'cat > /tmp/dnsmasq.leases' \  < backup/dnsmasq.leasesudhcpc -i eth0 -n -q -t 3 -s /tmp/hook.shudhcpc6 -i eth0 -n -q -t 3 -s /tmp/hook6.sh
Output
[client]$ udhcpc6 -i eth0 -n -q -t 3 -s /tmp/hook6.shudhcpc6: IPv6 obtained, lease time 3600[server2]$ cat /tmp/dnsmasq.leases1794252337 02:47:00:00:00:50 192.168.247.50 laptop1 01:02:47:00:00:00:50duid 00:01:00:01:32:84:cc:4b:32:4f:2d:35:02:741794252340 1165691437 fd47:247::146 * 00:03:00:01:02:47:00:00:00:501794252277 3932604784 fd47:247::109 * 00:03:00:01:02:47:00:00:00:50
  • IPv4: laptop1 got the same 192.168.247.50 from the new server 192.168.247.11 because the old lease was restored.
  • IPv6: udhcpc6 uses a new IAID each run, so DHCPv6 saw a new client and gave a new address (::146) - proof that identity = DUID + IAID.

247.8 Common mistakes

  • Treating Backup and Export as the same: Backup = database folder, Export = XML.
  • Exporting for migration without -Leases: the new server will not know the old leases.
  • Creating a v6 reservation with a MAC: DHCPv6 needs ClientDuid + Iaid.
  • Reading Get-DhcpServerv4Lease without -AllLeases and missing declined/bad leases.

247.9 Interview tips

  • Clients get no IP: Get-DhcpServerInDC + Get-DhcpServerv4Binding, then Get-DhcpServerv4Scope + Get-DhcpServerv4FreeIPAddress, then Get-DhcpServerv4Lease -AllLeases.
  • Same IPv6 every day for a laptop with a lease: Get-DhcpServerv6Lease | Add-DhcpServerv6Reservation; dnsmasq: dhcp-host.
  • DHCPv6 messages: Solicit, Advertise, Request, Reply; ports UDP 546/547 vs DHCPv4 67/68.
  • Migration: Export-DhcpServer -Leases on old, Import-DhcpServer on new.