Code on screen

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:

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

 $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
 $OpenFileDialog.initialDirectory = $initialDirectory
 $OpenFileDialog.filter = "All files (*.*)| *.*"
 $OpenFileDialog.ShowDialog() | Out-Null
#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
        $hostentry = [System.Net.Dns]::GetHostEntry($indivhost)
        $singletextresult = """$($hostentry.hostname)"""
        foreach ($address in $hostentry.addresslist)
            $singletextresult += ",""$($address.IPAddressToString)"""
        $textresults += $singletextresult
        $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")
Nick Shaw

Nick Shaw

I'm Nick, a knowledgeable and versatile security focussed network specialist. I have years of experience delivering complex projects for large and small organisations alike. As a full-stack engineer, I look at the end to end requirements and come up with a solution to match, rather than focussing just on one aspect. When I'm not working, I have three main interests: my family, football (Barnsley FC) and motorsports.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.