Powershell – Forward und Reverse Lookup Zonen Einträge prüfen

Mit diesem Skript lässt sich prüfen, ob es für einen Forward Eintrag den passenden Reverse Eintrag gibt. Basierend auf eine OU oder mit einem zusätzlichen Parameter -searchscope subtree auch die untergeordneten OUs.

$Path = "C:\Temp\ServerL.txt"
$Minutesback = "-10"
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddMinutes($Minutesback)
Get-ChildItem $Path | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item

$SearchBase = "OU=Server-PCI,OU=Server,OU=RZ,OU=SITES,DC=ndsedv,DC=de"
$computers = Get-ADComputer -Filter * -Properties * -SearchBase $SearchBase | Select-Object -Property dnshostName | Sort-Object -Property dnshostName | foreach { $_.dnshostName | Add-Content -Path C:\Temp\ServerL.txt}

$servers = Get-Content C:\Temp\ServerL.txt
foreach ($hostlist in $servers) {

 $hostlist | %{  
      $obj = "" | Select ComputerName,Ping,IPNumber,ForwardLookup,ReverseLookup,Result  
      $obj.ComputerName = $_  
   
      if(Test-Connection $_ -quiet){  
           $obj.Ping = "OK"  
     $obj.Result = "OK"  
      }  
      else{  
           $obj.Ping = "Fehler"  
     $obj.Result = "Fehler"  
      }  
        
      [array]$IPAddresses = [System.Net.Dns]::GetHostAddresses($obj.ComputerName) | ?{$_.AddressFamily -eq "InterNetwork"} | %{$_.IPAddressToString}  
   
      $obj.IPNumber = ($IPAddresses | measure).count  
        
   if($IPAddresses){  
     $obj.ForwardLookup = "OK"  
   
        $IPAddresses | %{  
             $tmpreverse = $null  
                  
             $tmpreverse = [System.Net.Dns]::GetHostByAddress($_).HostName  
             if($tmpreverse){  
                  
                  if($tmpreverse -ieq $obj.ComputerName){  
                       $obj.ReverseLookup += "$_ : OK `n"  
                  }  
                  else{  
                       $obj.ReverseLookup += "$_ wird aufgelöst als: $tmpreverse `n"  
                       $obj.Result = "Fehler"  
                  }  
             }  
             else{  
                  $obj.ReverseLookup = "Host nicht gefunden"  
                  $obj.Result = "Fehler"  
             }  
     }  
      }  
      else{  
           $obj.ForwardLookup = "IP nicht gefunden"  
           $obj.Result = "Fehler"  
      }  
        
      $obj  
      $obj | Export-csv "C:\Temp\DNSChecker_Result.csv" -append -NoTypeInformation
 }  }