Search This Blog

Tuesday, December 14, 2010

PowerShell One-liner to get IP address to hostname

Well, not really.

You could do this:
[System.Net.Dns]::GetHostAddresses("google.com")

Or create a script to help out little bit:

GetIP.ps1
param ($names  )

process {

if ($_ ) { $list = $_ }
if ($names ) { $list = $names  }



foreach ( $h in ( $list  ) )
{
    $row="" | select Name, IP;
    $i = [System.Net.Dns]::GetHostAddresses($h)  | ? { $_.AddressFamily -eq "Internetwork" } | % {$_.IpAddressToString }
    $row.Name = $h ;
    $row.IP = $i;
    if ($i) {
        $row ;
    }
    rv h, i
}

}


No comments:

Post a Comment