Performing an Asynchronous DNS Lookup Using PowerShell

This is another asynchronous approach to a common thing that a system administrator might do, much like a previous article I did on doing a speedy ping with PowerShell and async methods. I’ve already covered performance as well as handling each Task object so that won’t be covered here. Please review the linked blog for more information about those topics.

Instead of a ping, I am going to perform a lookup against DNS for an IP or hostname to get more information about a particular system or systems. The type that I am using to perform the DNS lookups is System.Net.Dns and the methods that I use are GetHostAddressesAsync() and GetHostEntryAsync().

Using both of these allows me to specify either an IPaddress or a hostname to perform the lookup against. Each item passed is tested to see if it is an IP address or not and then passed to the appropriate method that will be called with the data. Once that is done, it is passed to the Task watcher and then when everything has completed, you are presented with the results of each lookup.

The results of the command look something like this:

Get-DNSHostEntryAsync -Computername google.com,prox-hyperv,bing.com, github.com, `
powershellgallery.com, powershell.org,216.58.218.142

SNAGHTML87ff5b7

If you wanted to make this better, it is out on my GitHub repo so feel free to fork it and submit a PullRequest! Otherwise let me know what you think of it and enjoy!

Download Get-DNSHostEntryAsync

https://gallery.technet.microsoft.com/scriptcenter/Get-DNSHostEntryAsync-6fbd006c

About Boe Prox

Microsoft Cloud and Datacenter MVP working as a SQL DBA.
This entry was posted in powershell and tagged , , . Bookmark the permalink.

2 Responses to Performing an Asynchronous DNS Lookup Using PowerShell

  1. Robert Thomson says:

    I have submitted a pull request to GitHub with the proposed fix.

  2. Robert Thomson says:

    I could be wrong but I think you used the wrong variable for the foreach which would affect the pipeline processing case.

    The line in the end block
    $Task = ForEach ($Computer in $Computername) {
    Should be
    $Task = ForEach ($Computer in $Computerlist) {

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s