SkillPilotAI

PowerShell DhcpServer module (4/5)

Part 4 of the DhcpServer module: the remaining IPv4 and IPv6 Remove- cmdlets (reservations, scopes, option values, policies), Rename/Repair/Restore, and the server-level Set- cmdlets (audit log, database, DNS credential, settings, binding, class, DNS settings). The dnsmasq lab removes a reservation, an option, a class and a lease, then restores everything from a backup.

249.1 How remove and restore work

Reservation removed
  1. 1.Laptop MAC F0-DE-F1-7A-00-5E is reserved -> always 10.0.0.120

  2. 2.Admin PC runs Remove-DhcpServerv4Reservation on DHCP1

  3. 3.DHCP1 deletes the .120 reservation

  4. 4.Laptop broadcasts Discover

  5. 5.Offer: 10.0.0.101 from the normal pool

Restore after a mistake
  1. 1.Whole scope deleted by mistake

  2. 2.Restore-DhcpServer -Path dhcp\backup on DHCP1

  3. 3.Database with scope + reservation is back

  4. 4.Laptop sends Request

  5. 5.Ack: 10.0.0.120 again

Best practice: Backup-DhcpServer before any Remove

249.2 IPv4 Remove- cmdlets

PowerShell
Remove-DhcpServerv4Reservation `  -IPAddress 10.10.10.5, 10.10.10.6Remove-DhcpServerv4Scope `  -ScopeId 10.10.10.0, 10.20.20.0Remove-DhcpServerv4Superscope `  -SuperscopeName "Expanded Lab-4 Network" `  -ScopeId 10.1.1.0, 10.1.2.0Remove-DhcpServerv4OptionValue -OptionId 23 -ScopeId 10.10.10.0Remove-DhcpServerv4Policy `  -Name "IPPhonePolicy","PrinterPolicy"Remove-DhcpServerv4PolicyIPRange `  -ScopeId 10.10.10.0 -Name "HyperVPolicy" `  -StartRange 10.10.10.10 `  -EndRange 10.10.10.20Get-DhcpServerv4MulticastScope | Where-Object  { $_.State -Eq "Inactive" } |  Remove-DhcpServerv4MulticastScope -Force
CmdletWatch out
Remove-DhcpServerv4ReservationOnly -ScopeId = ALL reservations of the scope; -ScopeId -ClientId = one client
Remove-DhcpServerv4Scope-Force deletes even with active leases
Remove-DhcpServerv4SuperscopeWithout -ScopeId all scopes leave and the superscope is removed
Remove-DhcpServerv4OptionValueNo -ScopeId/-ReservedIP = server level; can't combine ScopeId + ReservedIP or UserClass + PolicyName
Remove-DhcpServerv4OptionDefinitionDeletes the definition itself (bigger step than a value)
Remove-DhcpServerv4PolicyWithout -ScopeId = server-level policies
Remove-DhcpServerv4PolicyIPRangeNeither Start nor End = ALL ranges of the policy

249.3 IPv6 Remove- cmdlets

PowerShell
Remove-DhcpServerv6Scope `  -Prefix 2001:4898:7020:1020:: -ForceRemove-DhcpServerv6Reservation `  -IPAddress 2001:4898:7020:1020::1Get-DhcpServerv6Scope |  Remove-DhcpServerv6LeaseRemove-DhcpServerv6Class `  -Name "User Class for Lab Computers" `  -Type "User"Remove-DhcpServerv6OptionValue `  -Prefix 2001:4898:7020:1020:: -OptionId 23
  • Same pattern as v4 but -Prefix instead of -ScopeId.
  • Removing a v6 scope also deletes all its settings and leases.
  • Only -Prefix on Reservation/Lease = every entry of that scope; ExclusionRange without a range = all ranges.
  • v6 Class needs -Name and -Type (Vendor|User); OptionDefinition/OptionValue need -OptionId.

249.4 Rename, Repair, Restore

PowerShell
Rename-DhcpServerv4Superscope `  -Name "Expanded B3 Network" `  -NewName "Expanded B3-1 Network"Repair-DhcpServerv4IPRecord `  -ScopeId 10.10.10.0 -ReportOnlyRestore-DhcpServer `  -ComputerName "dhcpserver.contoso.com" `  -Path "C:\Windows\system32\dhcp\backup"
  • Rename: -Name and -NewName are both required.
  • Repair-DhcpServerv4IPRecord reconciles inconsistent lease records; -ReportOnly reports without changing.
  • Restore-DhcpServer brings back the whole database from -Path; -Force skips the confirmation.

249.5 Server Set- cmdlets

PowerShell
Set-DhcpServerAuditLog -Enable $True `  -Path "D:\dhcpauditlog\" -MaxMBFileSize 100Set-DhcpServerDatabase `  -FileName "D:\NewDhcpPath\dhcp.mdb" `  -BackupPath "D:\NewDhcpPath\backup" `  -BackupInterval 30 -CleanupInterval 120$Credential = Get-CredentialSet-DhcpServerDnsCredential `  -Credential $Credential `  -ComputerName "DhcpServer03.Contoso.com"Set-DhcpServerSetting -NapEnabled $True `  -ConflictDetectionAttempts 1 `  -ActivatePolicies $FalseSet-DhcpServerv4Binding -BindingState $True `  -InterfaceAlias "Wired Ethernet Connection"Set-DhcpServerv4Class `  -Name "User Class for Lab Computers" `  -Type User -Data "LabComputers"Set-DhcpServerv4DnsSetting `  -DynamicUpdates "Always" `  -DeleteDnsRRonLeaseExpiry $True
CmdletKey options
Set-DhcpServerAuditLog-Enable, -Path, -MaxMBFileSize (all log files), -MinMBDiskSpace, -DiskCheckInterval
Set-DhcpServerDatabase-FileName, -BackupPath, -BackupInterval / -CleanupInterval (minutes), -RestoreFromBackup $true
Set-DhcpServerDnsCredentialAccount used to register client records in DNS
Set-DhcpServerSetting-ConflictDetectionAttempts, -ActivatePolicies, -NapEnabled, -NpsUnreachableAction
Set-DhcpServerv4Binding-BindingState + -InterfaceAlias (both required)
Set-DhcpServerv4Class-Data or -Description required
Set-DhcpServerv4DnsSetting-DynamicUpdates Always|Never|OnClientRequest, -DeleteDnsRRonLeaseExpiry, -NameProtection, -DnsSuffix; no level = server

249.6 Lab: dnsmasq setup

Output
dhcp-range=tag:!printer,192.168.249.100,  192.168.249.150,255.255.255.0,1hdhcp-vendorclass=set:printer,LabPrinterdhcp-range=tag:printer,192.168.249.40,  192.168.249.49,255.255.255.0,1hdhcp-hostsfile=/etc/n249-hostsdhcp-optsfile=/etc/n249-opts# /etc/n249-hosts  (reservation)02:49:00:00:00:50,192.168.249.50,laptop1# /etc/n249-opts  (option 15)option:domain-name,branch.lab
Output
[client]$ udhcpc -i eth0 -n -q -t 3 -T 2 -x hostname:laptop1 -s /tmp/hook.shudhcpc: lease of 192.168.249.50 obtained from 192.168.249.10, lease time 3600lease: ip=192.168.249.50 domain=branch.lab dns=192.168.249.10 lease=3600[client2]$ udhcpc -i eth0 -n -q -R -t 3 -T 2 -x hostname:printer1 -s /tmp/hook.sh -V LabPrinterudhcpc: lease of 192.168.249.46 obtained from 192.168.249.10, lease time 3600lease: ip=192.168.249.46 domain=branch.lab dns=192.168.249.10 lease=3600

249.7 Lab: remove reservation, option, class, lease

Terminal
# Remove-DhcpServerv4Reservation ideased -i '/02:49:00:00:00:50/d' /etc/n249-hostspkill -HUP dnsmasq# Remove-DhcpServerv4OptionValue -OptionId 15sed -i '/domain-name/d' /etc/n249-optspkill -HUP dnsmasq# Remove-DhcpServerv4Policy + PolicyIPRangesed -i -E '/^dhcp-vendorclass|tag:printer/d' \  /etc/n249-dnsmasq.confpkill dnsmasqdnsmasq -k -C /etc/n249-dnsmasq.conf \  --log-facility=/tmp/dnsmasq.log
Output
[client]$ udhcpc -i eth0 -n -q -t 3 -T 2 -x hostname:laptop1 -s /tmp/hook.shudhcpc: lease of 192.168.249.109 obtained from 192.168.249.10, lease time 3600lease: ip=192.168.249.109 domain=branch.lab dns=192.168.249.10 lease=3600[client]$ udhcpc -i eth0 -n -q -t 3 -T 2 -x hostname:laptop1 -s /tmp/hook.shudhcpc: lease of 192.168.249.109 obtained from 192.168.249.10, lease time 3600lease: ip=192.168.249.109 domain=none dns=192.168.249.10 lease=3600[client2]$ udhcpc -i eth0 -n -q -R -t 3 -T 2 -x hostname:printer1 -s /tmp/hook.sh -V LabPrinterudhcpc: lease of 192.168.249.110 obtained from 192.168.249.10, lease time 3600lease: ip=192.168.249.110 domain=none dns=192.168.249.10 lease=3600dnsmasq-dhcp[188]: 1070675817 vendor class: LabPrinterdnsmasq-dhcp[188]: 1070675817 tags: eth1dnsmasq-dhcp[188]: 1070675817 DHCPACK(eth1) 192.168.249.110 02:49:00:00:00:51 printer1
  • Reservation line deleted + SIGHUP: laptop1 got .109 from the pool (no known tag).
  • Option 15 line deleted + SIGHUP: same IP but domain=none; option 6 (DNS) still comes.
  • Class + printer range are in the main config, which SIGHUP does not re-read, so dnsmasq was restarted; printer1 still sent LabPrinter but got .110 from the normal pool.
  • Lease: laptop1 left but its .109 lease stayed, so guest1 asking for .109 got .111; after deleting the lease line (dnsmasq stopped) guest2 got .109.
Output
[server]$ sed -i '/02:49:00:00:00:50/d' /tmp/dnsmasq.leases; cat /tmp/dnsmasq.leases; echo '(lease file after delete)'1794254142 02:49:00:00:00:52 192.168.249.111 guest1 01:02:49:00:00:00:521794254124 02:49:00:00:00:51 192.168.249.110 printer1 01:02:49:00:00:00:51(lease file after delete)[client2]$ udhcpc -i eth0 -n -q -R -t 3 -T 2 -x hostname:guest2 -s /tmp/hook.sh -r 192.168.249.109udhcpc: lease of 192.168.249.109 obtained from 192.168.249.10, lease time 3600

249.8 Lab: restore from backup

Terminal
pkill dnsmasqdocker exec -i n249-server sh -c \  'cat > /etc/n249-hosts' < backup/n249-hosts# same for n249-opts, n249-dnsmasq.conf# and dnsmasq.leasesdnsmasq -k -C /etc/n249-dnsmasq.conf ...docker start n249-client   # laptop1 back
Output
[client]$ udhcpc -i eth0 -n -q -t 3 -T 2 -x hostname:laptop1 -s /tmp/hook.shudhcpc: lease of 192.168.249.50 obtained from 192.168.249.10, lease time 3600lease: ip=192.168.249.50 domain=branch.lab dns=192.168.249.10 lease=3600[client2]$ udhcpc -i eth0 -n -q -R -t 3 -T 2 -x hostname:printer1 -s /tmp/hook.sh -V LabPrinterudhcpc: lease of 192.168.249.46 obtained from 192.168.249.10, lease time 3600lease: ip=192.168.249.46 domain=branch.lab dns=192.168.249.10 lease=3600
  • The four backup files (config, hosts, opts, leases) were taken BEFORE the removes; copying them back undid every change.

249.9 Common mistakes

  • Thinking removing a scope removes only its name: settings and leases go too.
  • Removing a reservation and expecting a new IP at once: the active lease remains until removed or expired.
  • Giving only -ScopeId to Remove-DhcpServerv4Reservation and losing all reservations.
  • Not taking Backup-DhcpServer before bulk removes.

249.10 Interview tips

  • Reservation removed but same old IP: the active lease is still there; Remove-DhcpServerv4Lease or let it expire.
  • Scope deleted by mistake: Restore-DhcpServer -Path (BackupPath from Set-DhcpServerDatabase), then Repair-DhcpServerv4IPRecord -ReportOnly, then verify with Get-DhcpServerv4Scope / Lease.
  • v4 uses -ScopeId, v6 uses -Prefix in every Remove-.
  • Option value levels: server, scope (-ScopeId), reservation (-ReservedIP), class, policy.