Nikita Hlushak
Nikita Hlushak

Reputation: 21

Getting hostnames for ip-adresses using nslookup in Powershell

I'm actually zero to Powershell scripting, but I need to write one, to go through a .txt file with ip-adresses wrote in one per line and run nslookup for each adress to get it's hostname and write it in file (or get an error message if there is no DNS record for used IP).

I found this topic this almost the same task: Getting IP addresses for hostnames using nslookup in Powershell

so I need a similar solution, but using IP-adresses as input to get hostnames (or error message) in output.

Does anyone able to help me?

Thank you.

Upvotes: 1

Views: 5046

Answers (2)

mjolinor
mjolinor

Reputation: 68273

The GetHostBy* methods of System.Net.DNS have been deprecated and marked Obsolete.

Use GetHostEntry:

    foreach ($ipAddress in $ipAddresses) {
    [Net.Dns]::GetHostEntry($ipaddress).HostName
}

Upvotes: 1

Martin Brandl
Martin Brandl

Reputation: 58931

Use the GetHosetByAddress method and access the HostName property to retrieve the HostName:

$ipAddresses = get-content "path_to_the_file" 

foreach ($ipAddress in $ipAddresses) {
    [System.Net.Dns]::GetHostByAddress($ipAddress).HostName
}

Upvotes: 0

Related Questions