$filePath = "hosts.txt"
$exportPath = "output.csv"
$hostnames = Get-Content $filePath
$table = @()
foreach ($hostname in $hostnames) {
try {
$ipAddress = [System.Net.Dns]::GetHostAddresses($hostname) | Select-Object -ExpandProperty IPAddressToString
$table += [PSCustomObject]@{
Hostname = $hostname
IPAddress = $ipAddress
}
}
catch {
$table += [PSCustomObject]@{
Hostname = $hostname
IPAddress = 'N/A'
}
}
}
# Sort the table by IP address
$table = $table | Sort-Object IPAddress
# Export the sorted table to CSV
$table | Export-Csv -Path $exportPath -NoTypeInformation
It's good!