2016-03-24 21 views
1

Ben bir dosyadan konak isimlerine almak için biraz modifiye bu senaryoyu bulundu:.NET DNS sınıfı powershell arka plan işi mümkün mü?

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

Bu dosyadaki her ismi üzerinden hemen hemen yineler, bu bir dizeye döndü IP dönüştürür ping ve IP üzerinde geriye doğru bir arama yapar. Çalışıyor, ancak performansa ulaşmak için 600'den fazla makineye sahip olduğumda bueno yok. Test-connection ile bildiğim, saniyeler içinde forwardlookup işini yapan asenkron işlerin çalışabilmesi için kullanabileceğim bir asal parametresi var ama herkes bu davranışı tersine çevirmek için bir yol biliyor mu?

Bu forumda .NET işlem sınıfını kullanarak benzer bir şey yapabileceğinizi öne süren bir ileti buldum ancak birkaç aydır Powershell ile çalışıyorum ve MSDN belgelerini deşifre edemiyorum.

+1

https://gallery.technet.microsoft.com/scriptcenter/Run-Parallel-Parallel-377fd430 –

+0

@MickyBalladelli 'Invoke-Parallel -scriptfile c: \ work \ reverse.ps1 - işlemleri paralel hale getirmek için Invoke-Paralel'i kullanıyorum inputobject $ (get-content c: iş \ test.txt) -runspacetimeout 10 -throttle 10 'Bu satırı denemek ve test etmek için kullanıyorum. reverse.ps1 dosyasındaki kod şöyledir: '$ hostname = [System.Net.Dns] :: GetHostEntry ($ _). HostName $ results = @() foreach ($ hostname içinde $ host) {if ($) hostname) {$ results + = $ _ + "," + $ hostname} else {results + = $ _ + "," + "Ana Bilgisayar Adı bulunamadı"}} $ sonuç | dosya dışı c: \ work \ Results.txt' çıktı boş. Nerede yanlış gittiğimi görüyor musun? –

+0

Script dosyası yerine '-ScriptBlock' parametresini kullanmayı deneyin, nasıl kullandığımı. '-ImportVariables' parametresi de, runspac'ların tanımladığınız değişkenleri içe aktarmasına izin vermek açısından ilginçtir. –

cevap

1

durumda herkes ben aşağıdakileri yaparak sona erdi ilgileniyor:

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} 

Bu 4mins ilgili de üzerinde 450 + makineleri koştu.

1

Sen ileriye yönelik aşağıdakileri yapın ve arama ters foreach döngüsünde bir scriptblock içinde tamamlamayı ve Start-İş yapabilirsiniz: Örneğin

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

İşte temiz kopya:

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

Kaynak:

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

+0

serin, öğrenmeye çalışıyorum teşekkürler.Ben bir go ver ve daha iyi çalışıp çalışmadığına bakacağım –

+0

bu yüzden betiğin bir kısmını mantığın orijinal kaynak betiğiyle birleştirdim. İstediğim çıktıyı elde etmek için çalışıyordum ama betiği bir dosyayla test ettim Sadece 7 makine içeren ve bilgi almak için biraz zaman alıyor. Bunu 600'ün üzerinde makinede çalıştırmak istemiyorum. Daha kolay/daha hızlı bir yol olup olmadığını göreceğim. –

+0

@NiagNtawv ~ 560 makineyle benzer çalışmalar yaptım ve tamamlanma süresi yaklaşık 10-15 dakika. Ana kaynak Test-Bağlantıdır. $ PingOk mantık bloğundan kurtulabilirsiniz ve sadece ekrana gelen hatalara dikkat etmezseniz System.Net.DNS statik yöntemlerini ateşleyip unutabilirsiniz. Bu işlemleri paralel hale getirmek için Start-Job/Get-Job/Receive Job kullandığınızdan emin olun. Aksi halde, eğer bir foreach için bekliyorsan ... onları her seferinde bir tane yapıyorsun. –

İlgili konular