Resolve a List of IPs / Hosts to DNS Names

Carrying on from this post; I wanted to see how many of my dead clients were not resolvable to DNS names, which lead on to this tool. The star of this post is Al Feersum (not sure that’s his real name), since I borrowed his code and wrapped around it.

## TOOL FOR ADDING DNS LOOKUPS TO A LIST OF HOSTS ##
Param(
  [Parameter(Mandatory=$true)][String]$FileIn,
  [Parameter(Mandatory=$true)][String]$FileOut
)
FUNCTION ResolveAddress {
  Param($IP)
  TRY { $Resolved = [system.net.dns]::GetHostEntry([system.net.ipaddress]$IP).HostName }
  CATCH {
    TRY {
      $Resolved = (&nslookup $IP 2>$null)
      $Resolved = ($Resolved|where {$_ -match "^Name:"}).split(':')[1].trim()
    } CATCH { $Resolved = "Unable to resolve" }
  }
  RETURN $Resolved
} # From http://blog.forrestshields.com/post/2012/08/01/Resolving-Reverse-DNS-Lookups-in-PowerShell.aspx by Al Feersum!
[System.Array]$FileContent = Get-Content $FileIn
[System.Array]$NewFileContent = @()
$Counter = 1
$FileSize = $FileContent.Count
$FileContent | Foreach {
  Write-Host ([String]$Counter + " of " + [String]$FileSize)
  $DnsResult = ResolveAddress $_
  $NewFileContent += ($_ + "," + $DnsResult)   
  $Counter ++
}
$NewFileContent | Out-File $FileOut -Encoding Default


Comments