Do you have any way of getting IPs from a list of server names?

A few days ago, someone asked a very basic question. Where is the opposite version of this script:
http://enterpriseadmins.org/blog/scripting/do-you-have-any-way-of-getting-server-name-from-the-attached-list-of-ips/? They had a list of host names and wanted the associated IP addresses. Since it is somewhat common for a name to resolve to multiple IP addresses, this function has a few more lines as it will loop through each result.

Function Get-HostIP ([string]$hostName) {
  try {
    [system.net.dns]::GetHostByName($hostName).AddressList | %{
      New-Object psobject -Property @{
        HostName = $hostName
        IPAddress = $_.IPAddressToString
      } # End result object
    } # end foreach loop
  } catch {
    New-Object psobject -Property @{
      HostName = $hostName
      IPAddress = "Unknown/Error"
    } # end result object
  } # end try/catch
} # end function

Here is a sample result running this function against google.com:

Get-HostIP 'google.com'

IPAddress     HostName
---------     --------
173.194.46.67 google.com
173.194.46.71 google.com
173.194.46.65 google.com
173.194.46.66 google.com
173.194.46.69 google.com
173.194.46.72 google.com
173.194.46.70 google.com
173.194.46.78 google.com
173.194.46.68 google.com
173.194.46.73 google.com
173.194.46.64 google.com
This entry was posted in Scripting. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Notify me of followup comments via e-mail. You can also subscribe without commenting.