Friday, February 2, 2018

nslookup like functionality from Powershell

Below is a Powershell script that takes a list of aliases that you want to lookup. For each item in the list it will output a line. Each line is tab separated and composed of the alias, the host name, and the ip address (first one if there are multiples). The output can be copy and pasted into Excel easily. Any errors will be shown in Red.

$aliases = @(
'www.apple.com',
'www.apple.co.uk'
)

foreach ($alias in $aliases)
{
    try
    {
        $entry = [System.Net.DNS]::GetHostEntry($alias)
        $tab = [char]9
        $hostname = $entry.HostName
        $ipAddress = $entry.AddressList[0].IPAddressToString
        Write-Host "$alias$tab$hostname$tab$ipAddress"
    }
    catch 
    {
        Write-Host "$alias could not be processed" -ForegroundColor Red
    }    
}

There are actually Powershell packages that implement nslookup, but they require something be installed, imports, dependencies, etc. The only dependency to run this is that C# be installed and System.Net.DNS be available.

Example output is:

www.apple.com e6858.dsce9.akamaiedge.net 2.20.214.243
www.apple.co.uk apple.co.uk 17.172.224.108

7 comments:

Guru Guru said...


Thanks for making me to spent my 5 minutes us best.......
SAP MM Training in Chennai

Nishanth Kannan said...

Thanks for your wonderful information..
SAP Basis Training in Chennai

chenna sankar said...

Just read your website. Good one. I liked it. Keep going. you are a best writer your site is very useful and informative thanks for sharing!..

VMware Training in Chennai

buddy vino said...

I got lot of information from your blog. Keep on sharing like this blog...
SAP MM Training in Chennai

rp ap said...

Nice...Excellent article and useful to others

Skilful Training Classes and Boot Camps

rp ap said...

Good work and its really a useful info
AWS Training in chennai

Optimizing Search said...

Its really an Excellent post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog. posts. I hope you post again soon.

Selenium Training in Chennai