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: 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 {
    []::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

Get-HostIP ''

IPAddress     HostName
---------     --------
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.