I’ve been asked to prepare a simple tool which would:

  • Resolve IP address or DNS
  • Return all results to table
  • Import addresses from a file

The reason behind was to check how IP/DNS records change over the period of time, therefore there was a need for something like that:

  • A file with all addresses in question
  • Tool which could import all these addresses and resolve them
  • Save the results into a new file

Once having at least a couple of files, the results could be compared.

There are tools like ping, nslookup or Powershell’s Resolve-DnsName but they simply can’t meet all the requiments (or at least not to my knowledge).  So I simply needed to prepare the dedicated tool.

And thus I’ve created Resolve-IPDNS, the source code at the moment of writing this post looks like this:

Let’s break down the function now:

Since $IPDNS can take multiple values, I want to process the function for every address it is given.

The code above prepares the parameters which are going to be parsed to Resolve-DnsName . It establishes if the value is the IP address or DNS name “(IF ($addrs -as [ipaddress])” and then specifies the appropiate query type (Either A record for DNS or PTR record for IP).

It’s not really necessary, but gives more control over function.

Now, the script prepares properties for the new custom object. Since Powershell uses different property names for different queries, it’s essential to expand the right ones. As you can see above, for the IP address the IP and DNS properties are called Name and NameHost respectively, while for DNS they’re called IPAddress and Name, respectively.

By creating properties that way, I’m able to create an object with two consistent collums: IP and DNS which you can see bellow:

This is it, Powershell will output the nicely formated data onto your screen now:

As you can see, it can even deal with round robing entries!

You can obviously pipe the data into the function and export it to e.g. csv.

You can always find up-to-date version of the tool here: https://github.com/kprocyszyn

Main photo by: Jungwoo Hong on Unsplash