Skip to content

Commit

Permalink
Merge pull request #25 from sawft99/sawft99-patch-1
Browse files Browse the repository at this point in the history
Update DNSDupCheck.ps1
  • Loading branch information
sawft99 authored Nov 27, 2024
2 parents e4abda0 + cbd594a commit bef88df
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions DNSDupCheck.ps1
Original file line number Diff line number Diff line change
@@ -1,20 +1,40 @@
#Find duplicate dns entries
#Find duplicate DNS entries

$Domain = 'example.com'
$DNSServer = 'MyDNSServer'
$IncludeStaticRecords = $false

Clear-Host

#All A records, excluding static and some other special ones
$ARecords = Get-DnsServerResourceRecord -ComputerName $DNSServer -ZoneName $Domain -RRType A | Where-Object {($_.Timestamp -ne $null) -and ($_.HostName -ne '@') -and ($_.HostName -notmatch 'DnsZones') -and ($_.Hostname -notmatch $Domain)} | Select-Object -Property HostName,Timestamp,RecordData
Write-Host '
=====================
DNS Duplicate Records
====================='

#All A records, excluding some special ones
if ($IncludeStaticRecords -eq $false) {
$ARecords = Get-DnsServerResourceRecord -ComputerName $DNSServer -ZoneName $Domain -RRType A | Where-Object {($_.Timestamp -ne $null) -and ($_.HostName -ne '@') -and ($_.HostName -notmatch 'DnsZones') -and ($_.Hostname -notmatch $Domain)} | Select-Object -Property HostName,Timestamp,RecordData
} elseif ($IncludeStaticRecords -eq $true) {
$ARecords = Get-DnsServerResourceRecord -ComputerName $DNSServer -ZoneName $Domain -RRType A | Where-Object {($_.HostName -ne '@') -and ($_.HostName -notmatch 'DnsZones') -and ($_.Hostname -notmatch $Domain)} | Select-Object -Property HostName,Timestamp,RecordData
} else {
Write-Host ''
Write-Host -ForegroundColor Red '$IncludeStaticRecords needs to be $true or $false'
exit 2
}

#Create table with Name, timestamp, and IPv4 address
$ARecordsFormatted = $ARecords | Select-Object -Property HostName, Timestamp, @{Name='IPv4Address'; Expression={$_.RecordData.IPv4Address}}

#Group based on IP and only when the same IP is seen more than 2 times
#Group based on IP and only when the same IP is seen more than 1 time
$DuplicateNames = ($ARecordsFormatted | Group-Object -Property IPv4Address) | Where-Object -Property Count -gt 1

#Create table again and sort from least duplicates to most
$Final = $DuplicateNames | Select-Object Count, @{Name='IPv4Address'; Expression={$_.Name}},@{Name='HostNames'; Expression={$_.Group.HostName -join ', '}} | Sort-Object -Property Count

$Final
if ($null -ne $Final) {
$Final
} else {
Write-Host ''
Write-Host -ForegroundColor Green 'No duplicate records
'
}

0 comments on commit bef88df

Please sign in to comment.