fortypoundhead.com

Resolving IP to Hostname with PowerShell

Posted On 2016-01-15 by dwirch
Keywords:
Tags: Scripting Powershell Tip 
Views: 5618


Getting the hostname from an IP address (or vice versa) is no great magic, and can be done easily with PowerShell, by simply leveraging .Net to do the work.

We've all done something like this to get resolve an IP address:

[System.Net.Dns]::GetHostEntry("69.69.95.133").HostName

Or this to get an IP address from a hostname:

[System.Net.DNS]::GetHostAddresses("www.fortypoundhead.com").IPAddressToString

But did you ever notice that sea of red when you specify an invalid IP address or hostname? When you do that, you'll get a big,red error message similar to the following:

[System.Net.Dns]::gethostentry("128.254.95.254").HostName
Exception calling "GetHostEntry" with "1" argument(s): "No such host is known"
At line:1 char:1
+ [System.Net.Dns]::gethostentry("128.254.95.254").HostName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SocketException

I recently got asked how that nonsense could be avoided. Well, the answer is to simply encapsulate the process in a function that wraps up the .Net method to get the data back. It's pretty simple, and here is the code:

[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,Position=1)]
[string]$IPAddress
)
# noise if we fail

$ErrorActionPreference = "silentlycontinue"
$Result = $null

# Pass the IP to .Net for name resolution.

$result = [System.Net.Dns]::gethostentry($IPAddress)

# process the results

If ($Result)
{
$MyResult = [string]$Result.HostName
}
Else
{
$MyResult = "unresolved"
}

# Send it to the output

$MyResult

Alternatively, you could also plug this directly into a standalone script. For example, lets say that you have a list of IP addresses in a file that you want to resolve to host names. No problem: # Get list from file, initialize empty array

$ListOfIPs=Get-content ".\hosts.txt"
$ResultList = @()

# roll through the list, resolving as we
# go with the .Net DNS resolver

foreach ($IP in $ListOfIPs)
{
# We don't want to see any errors as we go, right?

$ErrorActionPreference = "silentlycontinue"
$Result = $null

# status to user, just so they know that something
# is still happening, then pass the current IP
# to .Net for name resolution.

write-host "resolving $IP"
$result = [System.Net.Dns]::gethostentry($IP)


# Enter into the array, with the returned results.

If ($Result)
{
$ResultList += "$IP," + [string]$Result.HostName
}
Else
{
$ResultList += "$IP,unresolved"
}
}

# send it out to a file, and inform the user we are done

$ResultList | Out-File .\resolved.txt
write-host "name resolution complete"

Again, pretty straightforward.

First, it grabs the list of IP addresses from a file called hosts.txt. Next, the script runs through the list, resolving the IP addresses (if it can), storing the results in $ResultList. Finally, the output is sent to another file called resolved.txt

I hope this is useful to someone, and drop a note in the comments or in the forums if you have questions.


About the Author

dwirch has posted a total of 185 articles.

 


Comments On This Post

By: AnonymousCoward
Date: 2016-08-29

Have all my upvotes. Reposting this to reddit. This was nearly impossible to find for servers still on powershell 2.0. Nice work


Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.


Your IP address is:54.81.195.240

Before you can post, you need to prove you are human. If you log in, this test goes away.




Recent Forum Posts

Advanced search added
dwirch posted on September 23, 2017 at about 13:44 in Site News

Job Spammer: Gaurav Mehta - AgreeYa Solutions
dwirch posted on September 22, 2017 at about 10:35 in Spammers

Job Spammer: Prutha Siri - Javelin Systems
dwirch posted on September 10, 2017 at about 6:15 in Spammers

New security implemented
dwirch posted on September 7, 2017 at about 7:16 in Site News

Malicious IP Checker Companion Tool
dwirch posted on August 12, 2017 at about 20:24 in Site News

Job Spammer: Steve Adams
dwirch posted on August 8, 2017 at about 7:44 in Spammers