Bulk DNS Lookup in Windows Powershell – Better than NSLookup!

So firstly, I've just seen that's its been over 3 months since I last posted - for anyone that looks back here regularly, Sorry! Thanks for coming back though, I am going to try to be more active.

I started a new job at the end of August as a Network Engineer as part of the network team in a data centre supporting multiple European sites. It's been a steep learning curve and has eaten most of my time. I've learnt more in the last three months than I thought possible, and loved every minute of it!

I've got a lot of things I could post about, but today I thought I'd start with a handy little script which I wrote in Windows Powershell which can take a list of host names (or IP's) from a text file, and perform a DNS Lookup on them.

In my job I quite often find myself with a whole bunch of host names that I need IP's for. I found it quite repetitive to keep opening a command prompt and typing "nslookup hostname", then copying and pasting it somewhere. So I started googling. I have never used Powershell before, so this was a good little project to learn a few things.

There's a few good things to be found by googling for an introduction to powershell, so I won't go over it here. The highlight is probably this article: Running Windows PowerShell Scripts which tells you how to actually run the scripts.

My scripts takes a text file as an input (which it opens a usual Windows "Open" dialog to let you choose), which needs to contain a list of hosts, one host per line. It will then perform a .NET DNS Lookup ([System.Net.Dns]::GetHostEntry), which is very similar to an nslookup but it returns an object which is more easily manipulated than the text based nslookup. It then outputs to a CSV file, with one line per entry containing all of the IP's returned (in the case of multiple DNS entries).

The script is available here: bulk_dns_lookup on GitLab

Included in the file is the PowerShell script, which I will past in full on the next page. There is also a sample hostlist.txt file. And finally, a shortcut. The shortcut runs the script, bypassing the PowerShell execution policy settings. It is just a shortcut to: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -executionpolicy bypass -Sta -File script_dns_lookup.ps1"

I hope this is useful for someone! :o)

As promised, the code:

:::powershell
# *****************************************************************
#   Bulk NSLOOKUP utility. Put a list of hosts in a text file, one
#                    per line, and run this file. 
#               Output will be in DNSLookup_Results.csv.
#
#                          Created by Nick Shaw
#                           Version 2. 25/11/13
# *****************************************************************

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")

Share this post

  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to LinkedIn
  • Share by Email