Powershell Resolve list of IP to host name


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                                                   Computer 1                                                                 ssmith                                                   Cannot resolve hostname                                        bjohn

Here is what I was able to find

I'm trying to do the opposite of that.

Who is Participating?

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

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.

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

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{
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.
You're welcome. Thanks too and good luck.
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

From novice to tech pro — start learning today.