[CmdletBinding()] param() # Copyright (C) 2014, Svendsen Tech. All rights reserved. # Joakim Borger Svendsen # 2014-08-15 # edited: 2014-11-08 $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 (there is no way to specify the DNS server here, # as far as I can tell). #### #### 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 }