Link to home
Start Free TrialLog in
Avatar of xouvang
xouvang

asked on

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
https://www.experts-exchange.com/questions/27741224/powershell-script-to-get-IPs-of-a-list-of-computer-hostnames.html

I'm trying to do the opposite of that.

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of zalazar
zalazar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of xouvang
xouvang

ASKER

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