Powershell pipe exporting to csv is loosing data

pipepowershellreverse-dnsscripting

I'm trying to write a script that will take input from a .txt file of IP address. Do a reverse DNS lookup to get the hostname of the IP address and then export that data into .csv file.

Here is what I have so far.

# getting the IP's from the file


$IPADDR = Get-Content "C:\Users\douglasfrancis\Desktop\IP_Test.txt"


ForEach ($IPADDR in $IPADDR)
{
  [System.Net.DNS]::GetHostbyAddress($IPADDR) | Add-Member -Name IP -Value $IPADDR -    MemberType NoteProperty -PassThru | select IP, HostName | sort -property Hostname | export-    csv "C:\Users\douglasfrancis\Desktop\ReverseLookup.csv" 

}

How it is now the created CSV file will have the column heads that I assigned and the last IP address in the list with its hostname. So somehow its dropping everything else.

If I run it with the export-csv commented out then all the IP's are displayed in the console but are not sorted by hostname.

I've used this same basic pipe before with no issues so I'm kinda at a loss for what is going on here. Any help would be awesome.

Thanks,

Best Answer

Took break and came back to it. Realized what I was missing and this now works as it should.

$SortIP =@()
ForEach ($IPADDR in $IPADDR)
{
  $SortIP += [System.Net.DNS]::GetHostbyAddress($IPADDR) | Add-Member -Name IP -Value     $IPADDR -MemberType NoteProperty -PassThru | select IP, HostName 
}

$SortIP | sort -property Hostname | export-csv "C:\Users\douglasfrancis\Desktop\ReverseLookup.csv" -NoTypeInformation

basically added the "$SortIP" var in and used "+= [System.Net.DNS]" instead of the "=[System.Net.DNS]" that I was using earlier and it does exactly what I expected it to.