导出到csv的Powershellpipe道正在丢失数据

我正在尝试编写一个脚本,它将从IP地址的.txt文件中获取input。 执行反向DNS查找以获取IP地址的主机名,然后将该数据导出到.csv文件中。

这是我迄今为止。

# 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" } 

现在如何创buildCSV文件将有我分配的列头和列表中的最后一个IP地址与其主机名。 所以不知何故它放弃了一切。

如果我用export-csv注释掉了它,那么所有的IP都显示在控制台中,但是不会按主机名sorting。

我以前用过这个基本pipe道,没有问题,所以我对这里发生的事情感到不知所措。 任何帮助都是极好的。

谢谢,

停下来,回到它。 意识到我错过了什么,现在它应该如此。

 $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 

基本上添加了“$ SortIP”var,并且使用了“+ = [System.Net.DNS]”,而不是之前使用的“= [System.Net.DNS]”,而且完全符合我的预期。

这对我有很大的帮助。 我一直试图添加到脚本中,以便它将logging没有DNS反向查找的条目。 在我正在运行的文件中,我有9个IP地址在2700的列表中没有反转。

到目前为止,我只能logging下成功。 我正在尝试的9个文件也包含这样的错误:

 ERROR: Exception calling "GetHostByAddress" with "1" argument(s): "The requested name is valid, but no data of the requested type was found" ERROR: At C:\SANDBOX\Reverse DNS Pipe fails CSV with SORT.ps1:21 char:48 ERROR: + $SortIP += [System.Net.DNS]::GetHostbyAddress <<<< ($IPADDR) | Add-Member -Name IP -Value $IPADDR -MemberType NoteProperty -PassThru | select IP, HostName ERROR: + CategoryInfo : NotSpecified: (:) [], MethodInvocationException ERROR: + FullyQualifiedErrorId : DotNetMethodException ERROR: 

这是我运行它的脚本:

 # getting the IP's from the file $IPADDR = Get-Content "C:\SANDBOX\inputips.txt" $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:\SANDBOX\ReverseLookupSORT.csv" -NoTypeInformation