PowerShell - IP to HostName resolve from text list
- Jon Boyette
- Jan 10, 2022
- 1 min read
Save as a named .ps1. This is the IP to host versus the latter, this one becomes invaluable if doing any traceroute items and need to list IP's out to resolve.
# Get list from file, initialize empty array
$ListOfIPs=Get-content "c:\temp\IPs.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 c:\temp\IPsresolved.txt
write-host "name resolution complete"
Comments