Jump to page sections
- Download Main Script
- Script Code
- Another Version That's not Necessarily Dependent on DnsShell
- Download (alternative version)
- Script Code (alternative version)
The main script uses the PowerShell module DnsShell from Codeplex (NB! Dead link! DnsShell is no longer on Codeplex and I think even Codeplex itself has been discontinued), which you will need to download to use that script, and it finds duplicate PTR records on a Windows DNS server.
DnsShell uses WMI calls. It was tested against a Windows Server 2008 R2 DNS server, but should also work against 2003 R2. Not tested against 2012 and up; feel free to provide feedback if you test it (svendsentech@gmail.com).I also tossed in a version that can be adapted to not rely on DnsShell (comment added 2023-05-18: And it's a good thing I did! Because DnsShell no longer is easily available), but you will then somehow need to get the reverse zones into a file or variable in the correct format. It can also be used with DnsShell to produce slightly different-looking results from what the main script does.
The script is written for "interactive use", so it displays results to the screen. You can edit it to suit your needs. It also exports a CSV if it finds any duplicate PTR records (apparently it always has a positive count, so it gets written anyway).Multiple host names for the same PTR record are displayed separated by a semicolon in the data.
It was tested with PowerShell v3, but should work with v2 (the latter being the default in Server 2008 R2 and Windows 7).Download Main Script
You will need to supply the -DnsServer parameter.
You can also tack on the -Verbose parameter for verbose output (this will also cause verbose output from the DnsShell module to show up).Script Code
[CmdletBinding()] param([Parameter(Mandatory=$true)][string] $DnsServer) # Copyright 2014, Svendsen Tech # All rights reserved. # Joakim Borger Svendsen # 2014-08-28Import-Module .\DnsShell # this has to exist ( http://dnsshell.codeplex.com/ )
$Zones = Get-DnsZone -Server $DnsServer $ReverseZones = $Zones | Where-Object { $_.ZoneName -like '*.arpa' } | Select-Object -ExpandProperty ZoneName # Example zone: # $ReverseZones = @('12.10.in-addr.arpa') $Dupes = @(foreach ($z in $ReverseZones) { Write-Verbose -Message "Processing zone: $z" $Records = Get-DnsRecord -ServerName $DnsServer -ZoneName $z -RecordType PTR $Records | ForEach-Object -Begin { $IpHash = @{} } -Process { $TempArray = $_.Name -replace '\.in-addr\.arpa$' -split '\.' $Ip = ($TempArray[-1..-($TempArray.Count)]) -join '.' if (-not $IpHash.ContainsKey($Ip)) { # New PTR. The Name property is the PTR. Create one-element array. $IpHash.$Ip = @($_.HostName) } else { # Duplicate/alias PTR, add to array. $IpHash.$Ip += $_.HostName } } $Duplicates = $IpHash.GetEnumerator() | Where-Object { $_.Value.Count -gt 1 } $Duplicates | ForEach-Object { New-Object psobject -Property @{ IP = $_.Name Duplicates = $_.Value -join '; ' } } }) # Display in console. $Dupes | Select IP, Duplicates | Format-Table -AutoSize # Export CSV file (if any dupes found). if ($Dupes.Count) { Write-Host -Fore Green 'Exporting CSV file PTRdupes.csv' $Dupes | Select IP, Duplicates | Export-Csv -Encoding UTF8 -NoTypeInformation -Path PTRdupes.csv -Delimiter ';' }
Another Version That's not Necessarily Dependent on DnsShell
> $Var = .\Get-DupePTR.ps1 ... > $Var | Format-Table # or format-list, export-csv, or whatever sensible you want to do
Download (alternative version)
Script Code (alternative version)
[CmdletBinding()] param() # Copyright (C) 2014, Svendsen Tech. All rights reserved. # Joakim Borger Svendsen # 2014-08-15 # edited: 2014-11-08Windows Powershell DNS Networking$DnsServer = 'your.dns.server'
# This uses DnsShell to get the zones, but you can edit this to read # zones from a file, or whatever, to avoid DnsShell. # It uses dnscmd.exe below and parses its output, and then uses the # (apparently obsolete) [Net.Dns] class' GetHostByAddress() method # to look up the IP #### #### Zones must be in the form 58.10.in-addr.arpa (for 10.58.0.0/16). #### Import-Module .\DnsShell # this has to exist ( http://dnsshell.codeplex.com/ ) $Zones = Get-DnsZone -Server $DnsServer $ReverseZones = $Zones | Where { $_.ZoneName -like '*.arpa' } | Select -ExpandProperty ZoneName # @('58.10.in-addr.arpa') # = foreach ($z in $ReverseZones) { Write-Host -Fore Green "Processing zone: $z" -NoNewline $Records = dnscmd.exe $DnsServer /ZonePrint $z # Get all records and parse them looking for duplicates below. #Write-Host -Fore Yellow " -- Num lines:" $Records.Count $Records | ForEach-Object -Begin { $IpHash = @{}; $Ctr = 0 } ` -Process { if ($_ -match '^([\d.]+)\s+.*PTR\s+(\S+)\.$') { $IpEndReversed = $matches[1] #Write-Verbose $IpEndReversed $Ctr++ Start-Sleep -Milliseconds 25 $TempArray = $z -replace '\.in-addr\.arpa$' -split '\.' $IpStart = ($TempArray[-1..-($TempArray.Count)]) -join '.' $TempArray = $IpEndReversed -split '\.' $IpEnd = ($TempArray[-1..-($TempArray.Count)]) -join '.' #Write-Verbose $IpEnd $Ip = "$IpStart.$IpEnd" #Write-Verbose "Looking up $Ip..." $ErrorActionPreference = 'Stop' try { $Dns = [Net.Dns]::GetHostByAddress($Ip) # This means we found a duplicate. Send an object down the pipeline. if ($Dns.Aliases -match '\S') { New-Object psobject -Property @{ IP = $Ip HostName = $Dns.HostName -join ', ' Aliases = $Dns.Aliases -join ', ' Error = $null } } } catch { New-Object psobject -Property @{ IP = $Ip HostName = $null Aliases = $null Error = $_ } } $ErrorActionPreference = 'Continue' } } Write-Host -Fore Yellow " -- Num records:" $Ctr #Write-Verbose "Processed $Ctr PTR records for $z" #Start-Sleep -Seconds 3 }
Blog articles in alphabetical order
A
- A Look at the KLP AksjeNorden Index Mutual Fund
- A primitive hex version of the seq gnu utility, written in perl
- Accessing the Bing Search API v5 using PowerShell
- Accessing the Google Custom Search API using PowerShell
- Active directory password expiration notification
- Aksje-, fonds- og ETF-utbytterapportgenerator for Nordnet-transaksjonslogg
- Ascii art characters powershell script
- Automatically delete old IIS logs with PowerShell
C
- Calculate and enumerate subnets with PSipcalc
- Calculate the trend for financial products based on close rates
- Check for open TCP ports using PowerShell
- Check if an AD user exists with Get-ADUser
- Check when servers were last patched with Windows Update via COM or WSUS
- Compiling or packaging an executable from perl code on windows
- Convert between Windows and Unix epoch with Python and Perl
- Convert file encoding using linux and iconv
- Convert from most encodings to utf8 with powershell
- ConvertTo-Json for PowerShell version 2
- Create cryptographically secure and pseudorandom data with PowerShell
- Crypto is here - and it is not going away
- Crypto logo analysis ftw
D
G
- Get rid of Psychology in the Stock Markets
- Get Folder Size with PowerShell, Blazingly Fast
- Get Linux disk space report in PowerShell
- Get-Weather cmdlet for PowerShell, using the OpenWeatherMap API
- Get-wmiobject wrapper
- Getting computer information using powershell
- Getting computer models in a domain using Powershell
- Getting computer names from AD using Powershell
- Getting usernames from active directory with powershell
- Gnu seq on steroids with hex support and descending ranges
- Gullpriser hos Gullbanken mot spotprisen til gull
H
- Have PowerShell trigger an action when CPU or memory usage reaches certain values
- Historical view of the SnP 500 Index since 1927, when corona is rampant in mid-March 2020
- How to check perl module version
- How to list all AD computer object properties
- Hva det innebærer at særkravet for lån til sekundærbolig bortfaller
I
L
M
P
- Parse openssl certificate date output into .NET DateTime objects
- Parse PsLoggedOn.exe Output with PowerShell
- Parse schtasks.exe Output with PowerShell
- Perl on windows
- Port scan subnets with PSnmap for PowerShell
- PowerShell Relative Strength Index (RSI) Calculator
- PowerShell .NET regex to validate IPv6 address (RFC-compliant)
- PowerShell benchmarking module built around Measure-Command
- Powershell change the wmi timeout value
- PowerShell check if file exists
- Powershell check if folder exists
- PowerShell Cmdlet for Splitting an Array
- PowerShell Executables File System Locations
- PowerShell foreach loops and ForEach-Object
- PowerShell Get-MountPointData Cmdlet
- PowerShell Java Auto-Update Script
- Powershell multi-line comments
- Powershell prompt for password convert securestring to plain text
- Powershell psexec wrapper
- PowerShell regex to accurately match IPv4 address (0-255 only)
- Powershell regular expressions
- Powershell split operator
- Powershell vs perl at text processing
- PS2CMD - embed PowerShell code in a batch file
R
- Recursively Remove Empty Folders, using PowerShell
- Remote control mom via PowerShell and TeamViewer
- Remove empty elements from an array in PowerShell
- Remove first or last n characters from a string in PowerShell
- Rename unix utility - windows port
- Renaming files using PowerShell
- Running perl one-liners and scripts from powershell
S
- Sammenlign gullpriser og sølvpriser hos norske forhandlere av edelmetall
- Self-contained batch file with perl code
- Simple Morningstar Fund Report Script
- Sort a list of computers by domain first and then name, using PowerShell
- Sort strings with numbers more humanely in PowerShell
- Sorting in ascending and descending order simultaneously in PowerShell
- Spar en slant med en optimalisert kredittkortportefølje
- Spre finansiell risiko på en skattesmart måte med flere Aksjesparekontoer
- SSH from PowerShell using the SSH.NET library
- SSH-Sessions Add-on with SCP SFTP Support
- Static Mutual Fund Portfolio the Last 2 Years Up 43 Percent
- STOXR - Currency Conversion Software - Open Exchange Rates API