我正在尝试编写一个脚本,它将从IP地址的.txt文件中获取输入。执行反向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"
}现在创建的CSV文件将具有我分配的列头和列表中的最后一个IP地址及其主机名。所以不知道怎么就放弃了其他一切。
如果我用注释掉的export运行它,那么所有的IP都会显示在控制台中,但不会按主机名排序。
我以前使用过同样的基本管道,没有任何问题,所以我对这里发生的事情有些不知所措。任何帮助都会很棒。
谢谢,
发布于 2014-05-02 05:41:30
在将结果导出到CSV之前,应该将结果放入数组中:
# getting the IP's from the file
$IPADDRS = Get-Content "C:\Users\douglasfrancis\Desktop\IP_Test.txt"
$result = @()
ForEach ($IPADDR in $IPADDRS)
{
$result += [System.Net.DNS]::GetHostbyAddress($IPADDR) | Add-Member -Name IP -Value $IPADDR -MemberType NoteProperty -PassThru | select IP, HostName
}
$result | sort -property Hostname | export-csv "C:\Users\douglasfrancis\Desktop\ReverseLookup.csv" -NoTypeInformation@David是真的,这不是您的麻烦,但是我使用$IPADDRS作为集合,对每个值使用$IPADDR,这样它是正确的(可读的)/
发布于 2014-05-02 05:40:54
在你的代码中,我看到:
ForEach ($IPADDR in $IPADDR)这是非常错误的:您使用相同的变量作为集合和变量进行迭代。
使用:
$ipaddr | %{ [System.Net.DNS]::GetHostbyAddress($_) } | ...https://stackoverflow.com/questions/23420999
复制相似问题