我发现了这个脚本,我对其进行了一些修改以从文件中获取主机名:
http://tompaps.blogspot.com/2015/04/verify-forward-and-reverse-dns-records.html
它几乎会遍历文件中的每个名称,对其进行 ping 操作,将返回的 IP 转换为字符串,并对 IP 执行反向查找。它可以工作,但是当我有 600 多台机器要测试时,性能就没有那么好了。我知道通过测试连接,我可以使用 -asjob 参数来运行异步作业,该作业在几秒钟内完成正向查找作业,但有人知道如何模仿这种反向查找行为吗?
我在这个论坛上发现了一篇帖子,建议您可以使用 .NET 进程类执行类似的操作,但我刚刚使用 Powershell 几个月,似乎无法解读 MSDN 文档。
最佳答案
您可以将其包装在脚本 block 中,并在 foreach 循环中执行 Start-Job,以执行以下正向和反向查找操作:
$ComputerName= ‘computername here’
[System.Net.Dns]::GetHostAddresses(“$ComputerName”).IPAddressToString
$ComputerIPAddress = ‘that computer ip here'
[System.Net.Dns]::GetHostEntry($ComputerIPAddress).HostName
例如
$whateverlist = Get-Content .\yourlistofservers.txt
# or you can..
$whateverlist = @"
machine1
machine2
machine3
etc
"@
$Scriptblock = {
param($machine);
$pingOk = Test-Connection -cn $machine -BufferSize 16 -Count 1 -EA silentlyContinue
if ($pingOk)
{
# Do whatever if it responds to pinging
# Maybe store the property in a list, put it out to a file etc.
[System.Net.Dns]::GetHostAddresses(“$machine”).IPAddressToString
# Use whatever method you like to get IP of the computer, even use the above output.
# Me being lazy:
$ip = [System.Net.Dns]::GetHostAddresses(“$machine”).IPAddressToString
[System.Net.Dns]::GetHostEntry($ip).HostName
}
}
# Then you can get the job, do whatever. Do it in a foreach for best results.
foreach ($machine in $whateverlist)
{
Start-Job -ScriptBlock $Scriptblock -ArgumentList $machine
}
# To crack open the eggs and get the goodies:
Receive-Job * -Keep | Out-File ".\whatevermanijustworkhere.txt"
这是一个干净的副本:
$whateverlist = Get-Content .\yourlistofservers.txt
$whateverlist = @"
machine1
machine2
machine3
etc
"@
$Scriptblock = {
param($machine);
$pingOk = Test-Connection -cn $machine -BufferSize 16 -Count 1 -EA silentlyContinue
if ($pingOk)
{
$ip = [System.Net.Dns]::GetHostAddresses(“$machine”).IPAddressToString
$ip
[System.Net.Dns]::GetHostEntry($ip).HostName
}
}
foreach ($machine in $whateverlist)
{
Start-Job -ScriptBlock $Scriptblock -ArgumentList $machine
}
Receive-Job * -Keep | Out-File ".\whatevermanijustworkhere.txt"
来源:
关于.NET DNS 类 powershell 后台作业可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36205922/