Skip to content

Instantly share code, notes, and snippets.

@markbosky
Created February 8, 2019 22:28
Show Gist options
  • Save markbosky/51b7208535d9580dce456352c9af035b to your computer and use it in GitHub Desktop.
Save markbosky/51b7208535d9580dce456352c9af035b to your computer and use it in GitHub Desktop.
DNS Bulk Lookup (PowerShell Script)
Function Get-FileName($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "All files (*.*)| *.*"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
} #end function Get-FileName
$path = Get-FileName
if (-Not($path)) { exit }
$hosts = Get-Content $path
$totalhosts = $hosts.length
echo "Looking up DNS records for $totalhosts hosts...please be patient..."
$textresults = @()
$hostcounter = 1
foreach ($indivhost in $hosts)
{
echo "Looking up host $hostcounter of $totalhosts"
$hostcounter += 1
Try
{
$hostentry = [System.Net.Dns]::GetHostEntry($indivhost)
$singletextresult = """$($hostentry.hostname)"""
foreach ($address in $hostentry.addresslist)
{
$singletextresult += ",""$($address.IPAddressToString)"""
}
$textresults += $singletextresult
}
catch
{
$singletextresult = """$indivhost"",""Not Found"""
$textresults += $singletextresult
}
}
$savefilename = "DNSLookup_Results.csv"
$textresults | Out-File $savefilename -Encoding utf8
echo "DNS Lookups completed. Results are stored in $savefilename"
Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment