Powershell script to scan the whole range of IP address and resolve the DNS names if exist.

Hi All,

I've got a script that needs some help form you, basically what I want it to do is to:

1. scan the network within the range
2. if there is DNS name available, then resolve it
3. if no DNS names then leave it as it is (IP only).

how can i achieve that, the following script can only run once but not 255 times ?

Thanks.
$range=1..255 | %{"10.2.2.$_"}
$hostname = ([System.Net.Dns]::GetHostByAddress("$range")).HostName 
Test-Connection $hostname -Count 1 | sort Destination | ft -AutoSize

Open in new window

LVL 1
jjozAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vikas_madhusudanaCommented:
can you try using this


$range=1..255 | %{"10.2.2.$_"}
for ($i=1;$i -lt 256;$i=$i+1) {
$hostname = ([System.Net.Dns]::GetHostByAddress("$range[$i]")).HostName 
Test-Connection $hostname -Count 1 | sort Destination | ft -AutoSize
}

Open in new window

vikas_madhusudanaCommented:
sorry no double cotes for $range[$i]


$range=1..255 | %{"10.2.2.$_"}
for ($i=1;$i -lt 256;$i=$i+1) {
$hostname = ([System.Net.Dns]::GetHostByAddress($range[$i])).HostName 
Test-Connection $hostname -Count 1 | sort Destination | ft -AutoSize
}

Open in new window

jjozAuthor Commented:
thanks for the reply mate,

it seems the first line is unusable therefore can be removed :-)

but is it possible to remove the error message below:

so rather than displaying error message, it should just skipped it ?
Test-Connection : Testing connection to computer 'esxi01.domain.com' failed: A non-recoverable error occurred during a database lookup
At line:6 char:20
+     Test-Connection <<<<  $hostname -Count 1 | ft -AutoSize
    + CategoryInfo          : ResourceUnavailable: (esxi01.itree.com.au:String) [Test-Connection], PingException
    + FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand
 

Open in new window

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

vikas_madhusudanaCommented:
Trap the exception and continue.
$range=1..255 | %{"10.2.2.$_"}
for ($i=1;$i -lt 256;$i=$i+1) {
trap [Exception] { continue;}
$hostname = ([System.Net.Dns]::GetHostByAddress($range[$i])).HostName 
Test-Connection $hostname -Count 1 | sort Destination | ft -AutoSize
}

Open in new window

Chris DentPowerShell DeveloperCommented:

GetHostByAddress is obsolete, you should use GetHostAddresses.

For Test-Connection, you'd be better using the -Quiet flag, needs a spot of modification though.

If you want to shift outside of classful IP address ranges I can teach you how to cross subnet boundaries neatly :)

Chris
1..255 | %{
  $IP = "10.2.2.$_"
  $HostName = [Net.Dns]::GetHostEntry($IP).HostName
    
  If (Test-Connection $IP -Quiet -Count 1)
  {
    If ($HostName -ne $Null)
    {
      $HostName
    }
    Else
    {
      $IP
    }
  }
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris DentPowerShell DeveloperCommented:

You should use GetHostEntry, sorry, need to finish my coffee :)

Chris
jjozAuthor Commented:
Thanks for the reply man !
sctechsupportCommented:
What If I want to map IP to DNSname is there any variable I ca use in this script?

for example 192.168.1.1 ---APP1.contoso.com
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.