Bulk DNS Lookup in Windows Powershell – Better than NSLookup!

Windows PowerShell Icon

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

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)

Posted in Scripting and tagged , , , .

19 Comments

  1. Nick that is a great script….the network guys were threatening us that our VPNs were not properly allowing DHCP registration, which didnt make much sense…. I needed something fast…….this proved it to be hogwash, well that our VPNs were at not the issue, thanks!

  2. As far as I can tell, [System.Net.Dns]::GetHostEntry is not the same as ‘nslookup’. It’s more like ‘ping’ in that it uses the local DNS cache whereas ‘nslookup’ actually queries the DNS server. Pity.

  3. Hi Nick, Please need one PS script for bulk port query using this command ” portqry -n devicename.com -p tcp -e 445 “

  4. Hi Nick,

    i am trying to do an IP to FQDN map and i am pretty sure that each IP that i have hosts more than 1 FQDN. I have not been able to write a batch script where i can identify more than 1 FQDN per IP address. I would appreciate if you could help me out here. Thanks in advance.

    • Hi,

      I don’t think that would be possible. As far as I’m aware each IP can only have one reverse DNS entry. I think you can map multiple domains to one IP, but you can only map the reverse entry to one domain.

      Nick.

  5. Handy. I made modifications:

    Line 26 – added file header to initialization of array.
    Line 35 – added “indivhost” so that success result lines contain the original value not only the name.
    Line 44 – added a second “Not Found” so that there’s consistently at least 3 values per line (domain submitted for lookup, “Name” response as “indivhost”, IPs).

    I also replaced spaces with tabs.

Leave a Reply

Your email address will not be published. Required fields are marked *