Powershell Resolve list of IP to host name

Hi,

I have a list of IP addresses (around 400 IP addresses)  that I need to find the host name.
The script I have right now is

Get-Content C:\IP_Address.txt | ForEach-Object {([system.net.dns]::GetHostByAddress($_)).hostname >> c:\hostname.txt}

Open in new window


But this is not getting the results of what I want. That code will only output the host name if the IP address is valid. Some IP addresses are not valid. This gives me a hard time trying to find out what is what.

Is there a way to have powershell resolve ip to host name and find out which user last used that workstation? Is it possible to out put it in this format?

Client Address                                         Host Name                                               User
10.10.10.10                                                   Computer 1                                                                 ssmith
10.10.10.11                                                   Cannot resolve hostname                                        bjohn

Here is what I was able to find
http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_27741224.html

I'm trying to do the opposite of that.

Thanks!
xouvangAsked:
Who is Participating?
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.

zalazarCommented:
Can you try the following code please.

$strLogfile = "c:\hostname.txt"
ForEach ($sIP in Get-Content C:\IP_Address.txt)
{
  try {
    $sHostname = ([system.net.dns]::GetHostByAddress($sIP)).hostname
    Write-Host "INFO|$($sIP)|${sHostname}"
    Out-File -FilePath $strLogfile -Append -Encoding ASCII -InputObject "INFO|$($sIP)|${sHostname}"
  } catch {
    Write-Host "ERROR|${sIP}|Cannot resolve IP-address"
    Out-File -FilePath $strLogfile -Append -Encoding ASCII -InputObject "ERROR|${sIP}|Cannot resolve IP-address"
  }
}

Open in new window

0

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
xouvangAuthor Commented:
Thank you that code is similar to the one I was working and modified

get-content C:\ip.txt | foreach-object{
$a=([system.net.Dns]::GetHostByAddress($_)).hostname
if($? -eq $False) {add-content -path C:\IP.csv -value "$_,Cannot resolve hostname"}
elseif($? -eq $True){add-content -path C:\IP.csv -value "$_,$a"}
}

Open in new window


I guess I'll go ahead and make another script to get last logged users.
Thanks!
0
zalazarCommented:
You're welcome. Thanks too and good luck.
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.