Skip to content

Instantly share code, notes, and snippets.

@nathanpc
Last active October 8, 2023 09:32
Show Gist options
  • Save nathanpc/d96583fba5e505468a18d96b60ae00a0 to your computer and use it in GitHub Desktop.
Save nathanpc/d96583fba5e505468a18d96b60ae00a0 to your computer and use it in GitHub Desktop.
Script to update the local DNS records based on the DHCP server reservations
# Reset-DnsLocalAddresses
# Clears and repopulates the automatically managed DNS entries in the server.
#
# Author: Nathan Campos <[email protected]>
# Parameters.
[String]$ZoneName = "something.lan"
[IPAddress]$DhcpScope = 192.168.1.0
Function Get-DhcpReservations {
Get-DhcpServerv4Reservation -ComputerName $env:ComputerName -ScopeId $DhcpScope |
Select-Object Name,IPAddress |
Sort-Object -Property { [System.Version]$_.IPAddress }
}
Function Clear-DnsRecords {
Get-DnsServerResourceRecord -ZoneName $ZoneName -RRType "A" |
Remove-DnsServerResourceRecord -ZoneName $ZoneName -Force
}
Function Add-DnsReservations {
Get-DhcpReservations |
ForEach { New-Object PSObject -Property @{ Name = $_.Name.Split(".")[0].ToLower() -replace "[^a-z0-9-]",""; IPAddress = $_.IPAddress } } |
ForEach { Add-DnsServerResourceRecordA -Name $_.Name -IPv4Address $_.IPAddress -ZoneName $ZoneName }
}
Clear-DnsRecords
Add-DnsReservations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment