reverse dns lookup get ip and name results together...

I’m performing a reverse dns lookup but I only get the name and not the IP used in my results. I would like to have both so I can match one to one. Can you help?

#Reverse IP Lookup
$IP1 = “10.11.162.15”,
“172.29.65.228”

$PC = foreach ($p in $IP1)
{
[System.Net.Dns]::GetHostEntry($p).HostName | select-object $p, $p.hostname
}

$PC

My select object statement does not appear to be bringing in the data I need and it appears to be from the reverse dns lookup command I’m using.

 

you’re close…

foreach ($p in $ip1){
$pc=([System.Net.Dns]::GetHostEntry($p).HostName)
write-host "IP = $P, DNS =$PC"}

Nice and thank you but…

Is there a way to get it collected for foreach so that I can export to a csv?

Yes there is. There’s a cmdlet called export-csv. Look for some examples online, and then give it a shot. If you’re still having issues post your script and the error(s).

an0nemus09
I appreciate all the help and what was first provided works great but wanted to say that I'm fully aware of the export-csv function but if you look at my first command this is usually how I get all the collected data to then be able to export it.

I can’t do the export inside the foreach because then it will just write over the file with every IP so I usually put the entire foreach to a variable and then pipe to export-csv. My issue is in getting these variables out are causing me the trouble. I have done this before with no problem but for some reason this command is giving me trouble. I’m sure I’m close but for some reason just not getting it.

[pre]$pc=foreach ($p in $ip1){
$pc1=([System.Net.Dns]::GetHostEntry($p).HostName)

$pc1 | select-object $p, $pc1
write-host “IP = $P, DNS =$PC1”}
$PC | export-csv -path c:\file.csv$ [/pre]

foreach ($p in $ip1){
$pc=([System.Net.Dns]::GetHostEntry($p).HostName)
"$pc,$p"|add-content myout3.csv
}
$IP1 = '10.11.162.15','172.29.65.228'
$result = 
foreach ($ip in $IP1){
    Write-Verbose "Resolving $ip" -Verbose
    $dns = [System.Net.Dns]::GetHostEntry($ip)
    $dns | Select-Object HostName,
    @{n='AddressList';exp={$_.AddressList.IPAddressToString}}
}

$result | export-csv .\dnsresult.csv -NoTypeInformation