2016-03-24 6 views
1

私はファイルからホスト名に取るようにビットを変更し、このスクリプトが見つかりました:それは、文字列に返されたIPに変換ピング、ファイルにそれぞれの名前によってそれはかなり反復し.NET DNSクラスのpowershellのバックグラウンドジョブは可能ですか?

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

をIP上で逆引き参照を実行する。それは動作しますが、パフォーマンスが向上するためには600台以上のマシンが必要です。テスト接続の場合、forwardlookupジョブを秒単位で実行する非同期ジョブを実行するために使用できる-asjobパラメータがありますが、逆引き参照のためにこの動作を模倣する方法は誰にも分かりますか?

.NETのプロセスクラスを使用して同様のことを行うことができますが、Powershellを使用して数ヶ月間作業していて、MSDNのドキュメントを解読できないように思われる投稿が見つかりました。あなたがスクリプトブロックでそれを包むことができ、前進のために次の操作を実行し、逆引き参照するforeachループで、ジョブを開始します

+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($ホスト名$ホスト){if($ホスト名が見つかりません "}} $ result | = $ _ +"、 "+ホスト名が見つかりません" out-file c:\ work \ Results.txt'の出力は空白です。ここでどこが間違っているのか分かりますか? –

+0

scriptfileの代わりに '-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} 

これは4mins程度で450以上のオーバーマシンを実行しました。

1

:たとえば

$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分です。主なリソースはTest-Connectionです。 $ pingOkロジックブロックを取り除くことができ、スクリーンに溢れるエラーを気にしない場合は、System.Net.DNS静的メソッドを起動して忘れてしまいます。 これらの操作を並列化するには、Start-Job/Get-Job/Receive Jobを使用していることを確認してください。それ以外の場合は、foreachを待っている場合...あなたは一度に1つずつやっています。 –

関連する問題