Resolving Host Names

by Dec 8, 2010

In a previous tip, you learned how to quickly ping network segments. Next, you could resolve online IPs and get host lists of systems currently available:

function Check-Online {
param(
$computername
)
test-connection -count 1 -ComputerName $computername -TimeToLive 5 -asJob |
Wait-Job |
Receive-Job |
Where-Object { $_.StatusCode -eq 0 } |
Select-Object -ExpandProperty Address
}
$ips = 1..255 | ForEach-Object { "10.10.10.$_" }
$online = Check-Online -computername $ips
$online |
Sort-Object |
ForEach-Object {
$ip = $_
try {
[System.Net.Dns]::GetHostByAddress($ip)
}
catch {
"cannot resolve $ip. Reason: $_"
}
}

Twitter This Tip!
ReTweet this Tip!