fortypoundhead.com

Resolving IP to Hostname with PowerShell

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


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 174 articles.

You can find more information from dwirch by visiting http://www.derekwirch.com.


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.156.39.44

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



Recent Forum Posts

New security implemented
dwirch posted on July 23, 2017 at about 6:58 in Site News

Fold Code Manager into main KB?
VB6Boy posted on July 22, 2017 at about 14:42 in Site News

Fold Code Manager into main KB?
dwirch posted on July 22, 2017 at about 14:41 in Site News

Fold Code Manager into main KB?
dwirch posted on July 21, 2017 at about 22:46 in Site News

Fold Code Manager into main KB?
dwirch posted on July 20, 2017 at about 7:55 in Site News

Job Spammer: Sam Mallon
dwirch posted on July 18, 2017 at about 18:36 in Spammers