Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

PowerShell Wrapper for Nslookup with error handling, basically NSlookup on Steroids...

$
0
0

I have been asked on a few of the projects that I have been working on to take a list of servers and get the FQDN and IP’s. 

 

I created a tool for this that if you need it, I can share with you.

 

It is like a Nslookup on steroids.  If you use Nslookup, you can only do one server at a time and you can’t control the format that the data come out in.  

 

PS C:\> nslookup server01tst 10.10.10.20

Server:  server531.dom.com

Address:  10.10.10.20

 

Name:    server532.dom.com

Address:  10.10.10.23

 

With my tool, you can pipe in (feed it information) and run as many computer names through it as you like.

 

Let me show a couple of examples:

 

Just basic use would look like this.

PS C:\> Get-DNSInfo -Computername server300 -DNSServerIP 10.10.10.20

 

ComputeName                  IpAddress  

-----------                              ---------  

server300.dom.com    10.0.0.9

 

PS C:\> Get-DNSInfo server300 10.10.10.20

 

ComputeName                  IpAddress  

-----------                               ---------  

server300.dom.com    10.0.0.9

 

Here is where is gets a lot more powerful.

 

Get-Content -Path C:\01servers\servers.txt | Get-DNSInfo -DNSServerIP '10.10.10.190' 

 

Now you are taking an unlimited list of servers and piping it into the command, no extra work

 

PS C:\> Get-Content -Path C:\01servers\servers.txt | Get-DNSInfo -DNSServerIP '10.10.10.190'

 

ComputeName                                                                         IpAddress                                                                          

-----------                                                                         ---------                                                                          

server300.test.dom.com                                                            10.0.11.133                                                                      

server300.dom.com                                                             10.0.0.9                                                                        

server302.dom.com                                                             10.0.0.33                                                                       

server309.nor.dom.com                                                             10.0.0.129                                                                      

server311                                                                         None                                                                               

server753.ess.com                                                                 10.0.0.121 

Truncated results

 

An you can take the Output into a csv file.

 

Get-Content -Path C:\01servers\servers.txt | Get-DNSInfo -DNSServerIP '10.10.10.190' | Export-Csv C:\01server\Dnsoutput.csv 

 

You can use any resource for the server list too, here I will use PowerCli (VMWare PowerShell tool) to get a list of VM, and pipe (feed into) the command.

 

PS C:\> Get-VM Server* | select @{N="Computername";E={$_.name}} | Get-DNSInfo 

 

ComputeName                                                                         IpAddress                                                                          

-----------                                                                         ---------                                                                          

server532.dom.com                                                             10.10.10.23                                                                      

atl01osi326.dom.com                                                             10.0.0.224                                                                      

atl01osi329.dom.com                                                             10.0.0.237                                                                      

atl01osi328                                                                         None                                                                               

server532-b                                                                       None      

 

 

You can create a module on your laptop and add this function and it will be available to you in PowerShell V3 and above automatically.  

Just type the command.

Here is the code if you would like:

 

 

<#

.Synopsis

   Get FQDN and IP for a list of servers.

.DESCRIPTION

   Get FQDN and IP for a single server, or a list of servers, specify the Ip of the DNS server if you don't want 10.10.10.20.

.PARAMETER ComputerName

 

.PARAMETER DNSServerIP

 

.EXAMPLE

   Get-Content C:\serverlist.txt | Get-DNSInfo | Export-CSV C:\ServerList.csv

.EXAMPLE

   Another example of how to use this cmdlet

#>

function Get-DNSInfo{

    [CmdletBinding()]

    Param(

        [Parameter(Mandatory=$true,

                   ValueFromPipeline=$true,

                   ValueFromPipelineByPropertyName=$true,

                   Position=0)]

                   [alias("Computer")]

                   [ValidateLength(3,35)]

        [string[]]$Computername,

        [Parameter(Position=1)]

        [string]$DNSServerIP = '10.10.10.20',

        [Parameter(Position=2)]

        [string] $ErrorFile = "C:\01server\Nfail.txt"

    )

 

    Begin{

        if(!(Test-Path C:\01server -PathType Container)){

            New-Item C:\01server -ItemType Directory | Out-Null

            New-Item C:\01server\Nfail.txt -ItemType file | Out-Null

        }#if

            $server = ""

            $IP = ""

            $object = [pscustomobject]@{}

 

    }#end begin

 

    Process{

        foreach($computer in $Computername){

            $Lookup = nslookup $computer $DNSServerIP 2> $ErrorFile 

                $Lookup | Where{$_} | foreach{

                    if(($Error[1].Exception.Message -split ':')[1] -eq ' Non-existent domain'){

                        $object | Add-Member ComputeName $computer

                        $object | Add-Member IpAddress "None"

                        $object

                        $object = [pscustomobject]@{}

                        Write-Error "End" 2>> $ErrorFile

                    }elseif($_ -match "^Name:\s+(?<name>.+)"){

                            $server = $Matches.name

                    }elseif($_ -match "$DNSServerIP"){

                    }elseif($_ -match "^Address:\s+(?<ipaddress>.+)"){

                            $IP = $Matches.ipaddress

                    }#if

                }#foreach

            $Lookup = ''

            $object | Add-Member ComputeName $server

            $object | Add-Member IpAddress $ip

            if($object.ComputeName){$object}

            $server = ''

            $ip = ''

            $object = [pscustomobject]@{}

 

        }#end foreach

    }#end process

 

    End{

    }#end end

}#end function 

 

 

 

By the way, it you have an IP, or list of IP’s and want to see if there is reverse lookup, you can do that too:

 

Get-DNSInfo 10.10.10.23 

 

ComputeName                                                                         IpAddress                                                                          

-----------                                                                         ---------                                                                          

server532.dom.com                                                             10.10.10.23                                                                      

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles