Script to convert IP to DNS and DNS to IP ?

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
ITEngineer
Posts: 145
Joined: Wed Oct 12, 2011 10:52 am

Script to convert IP to DNS and DNS to IP ?

Post by ITEngineer » Wed Oct 12, 2011 10:52 am

Hi Everyone,

I'd appreciate your efforts in helping me modifying the script below so that it can check & convert the list of IPAddress and hostname into something like:

DomainController1 - 10.1.1.10 - UP
CoreGatewayRTR1 - 10.1.1.254 - DOWN
JohnPC01 - NO-IP-Address - DOWN
LindaLaptop02 - 10.1.1.234 - DOWN
.
.
.

it is a challenge for me to convert IP to DNSName and DNSName to IP then check the online status based on Ping,

the following is the script that I can come up with so far:

############################################
$computers= gc C:Listv3Devices.txt
foreach ($computername in $computers) {
$DNS = [System.Net.Dns]::GetHostEntry($ComputerName)
$HostName = $DNS.HostName
$IP = $DNS.AddressList

Trap { Continue }
if (Test-Connection $DNS -erroraction SilentlyContinue -Count 1 ) {
write-host "$Hostname - $IP - UP" -ForegroundColor GREEN
}
else {
write-host "$Hostname - $IP - DOWN" -ForegroundColor RED
}
}
################################################

However, the above script still got minor logic error as follows:

1. all of the result is always displayed as down ?

2. some of the result is always duplicated even though the IP address of that particular host is only one ?

Any kind of help would be greatly appreciated.

Thanks.albertwt2011-10-12 18:37:04
/* IT Engineer */

User avatar
ITEngineer
Posts: 145
Joined: Wed Oct 12, 2011 10:52 am

Script to convert IP to DNS and DNS to IP ?

Post by ITEngineer » Wed Oct 12, 2011 11:25 am

Hi JVierra,

What I'd like to achieve is that I need to test the host/server availability using ping from the mixed list of IP address and hostname.
/* IT Engineer */

Locked