2016-03-24 13 views
1

我发现这个剧本,我修改了一下采取从文件中主机名:.NET DNS类的PowerShell后台工作可能吗?

http://tompaps.blogspot.com/2015/04/verify-forward-and-reverse-dns-records.html

它通过每一个名字几乎遍历文件中,坪它,返回的IP转换成字符串并对IP进行反向查找。它可以工作,但是当我有600多台机器可以通过性能时,它不是bueno。我知道用测试连接有一个-asjob参数,我可以用它来运行异步作业,它在几秒钟内完成向前查找作业,但有人知道一种方法来模拟反向查找的行为吗?

我在这个论坛上发现了一篇文章,建议您可以使用.NET进程类来做类似的事情,但我刚刚使用了Powershell几个月,似乎无法破译MSDN文档。

+1

我使用Invoke-Parallel来并行化动作https://gallery.technet.microsoft.com/scriptcenter/Run-Parallel-Parallel-377fd430 –

+0

@MickyBalladelli'Invoke-Parallel -scriptfile c:\ work \ reverse.ps1 - inputobject $(get-content c:work \ test.txt)-runspacetimeout 10 -throttle 10'我正在使用这一行来尝试和运行测试。在reverse.ps1文件中的代码如下:'$ hostname = [System.Net.Dns] :: GetHostEntry($ _)。HostName $ results = @()foreach($ hostname中的$ host){if($主机名){$ results + = $ _ +“,”+ $ hostname} else {results + = $ _ +“,”+“No Hostname Found”}} $ result | out-file c:\ work \ Results.txt'输出为空。你看到我要去哪里吗? –

+0

尝试使用'-ScriptBlock'参数代替脚本文件,这是我使用它的方式。 '-ImportVariables'参数对于让运行空间导入您定义的变量也很有意思。 –

回答

1

如果任何人的兴趣我最后做了以下内容:

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+机约4mins。

1

您可以在一个脚本块包起来,做好启动工作在foreach循环做以下的正向和反向查找:

$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" 

来源:

https://adsecurity.org/?p=305

+0

很酷,谢谢。我会给它一个去看看它是否更好 –

+0

所以我能够将您的脚本中的一些逻辑与原始源脚本结合起来,以获得我想要的输出,但是我使用文件测试了脚本只包含7台机器,并且需要一段时间才能获取信息。不想在600多台机器上运行。我会看看是否有更简单/更快的方法。 –

+0

@NiagNtawv我已经完成了约560台机器的类似工作,完成时间大约需要10-15分钟。主要资源是测试连接。您可以摆脱$ pingOk逻辑模块,只需发射并忘记System.Net.DNS静态方法(如果您不关心泛滥屏幕的错误)。 确保您使用Start-Job/Get-Job/Receive Job来并行化这些操作。否则,如果你正在等待foreach ...你一次只做一个。 –