.NET DNS类powershell后台可以工作吗? [英] .NET DNS class powershell background job possible?
问题描述
我发现这个脚本,我修改了一些文件中的主机名:
I found this script which I modified a bit to take in the hostnames from a file:
http://tompaps.blogspot.com/2015/04/verify-forward-and-reverse-dns-records .html
它几乎遍历文件中的每个名称,ping它,将返回的IP转换为字符串,并在IP上执行反向查找。它的工作原理,但是当我有600多台机器通过性能是没有bueno。我知道与测试连接有一个-asjob参数,我可以用来运行异步作业,这是在几秒钟内完成的前瞻性工作,但有谁知道一种方式来模拟这种反向查找的行为?
It pretty much iterates through each name in the file, pings it, converts the returned IP into a string and performs a reverse lookup on the IP. It works, but when I have 600+ machines to go through performance is no bueno. I know with test-connection there's an -asjob parameter I can use to run asynchronous jobs which does the forwardlookup job in seconds but does anyone know a way to mimic this behavior for the reverse lookup?
我在这个论坛上发现了一个帖子,建议您可以使用.NET进程类做类似的事情,但是我刚刚在Powershell工作了好几个月,似乎解密MSDN文档。
I found a post on this forum suggesting you can do something similar using the .NET process class but I've just been working with Powershell for a few months and can't seem to decipher the MSDN docs.
推荐答案
如果有人感兴趣,我最终做了以下事情:
In case anyone's interested I ended up doing the following:
Import-Module 'C:\Users\Lia Cha\Documents\Windows Powershell\Modules\Invoke-Parallel.psm1'
$machines = Get-Content C:\work\hostnames.txt
Invoke-Parallel -InputObject $machines -RunspaceTimeout 10 -Throttle 10 -ErrorAction SilentlyContinue -ScriptBlock {
$obj = "" | Select ComputerName,Ping,IPNumber,ForwardLookup,ReverseLookup,Result
$obj.ComputerName = $_
# ping each host
if(Test-Connection $_ -quiet -Count 1){
$obj.Ping = "OK"
$obj.Result = "OK"
}
else{
$obj.Ping = "Error"
$obj.Result = "Error"
}
# lookup IP addresses of the given host
[array]$IPAddresses = [System.Net.Dns]::GetHostAddresses($obj.ComputerName) | ?{$_.AddressFamily -eq "InterNetwork"} | %{$_.IPAddressToString}
# caputer count of IPs
$obj.IPNumber = ($IPAddresses | measure).count
# if there were IPs returned from DNS, go through each IP
if($IPAddresses){
$obj.ForwardLookup = "OK"
$IPAddresses | %{
$tmpreverse = $null
# perform reverse lookup on the given IP
$tmpreverse = [System.Net.Dns]::GetHostEntry($_).HostName
if($tmpreverse){
# if the returned host name is the same as the name being processed from the input, the result is OK
if($tmpreverse -ieq $obj.ComputerName){
$obj.ReverseLookup += "$_ : OK `n"
}
else{
$obj.ReverseLookup += "$_ different hostname: $tmpreverse `n"
$obj.Result = "Error"
}
}
else{
$obj.ReverseLookup = "No host found"
$obj.Result = "Error"
}
}
}
else{
$obj.ForwardLookup = "No IP found"
$obj.Result = "Error"
}
# return the output object
$obj | ft -AutoSize | out-string -width 4096 | out-file c:\work\Results.txt -Append}
这超过了450+机器约4分钟。
This ran over 450+ machines in about 4mins.
这篇关于.NET DNS类powershell后台可以工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!