title: speed up in DC's scanning
body: <p>I'm building a script which will go to each DC's and take the value from <code>modifyTimeStampe</code> values and then on take the maximum value as the following code, at the moment the code is too slow because now it goes to each domain and put the value to the array, then get the max value from the array out. I would like to speed it up. I was thinking to use multiple threads, but still looking for a better idea to implement.</p>
  
  <pre><code>$Searcher = New-Object System.DirectoryServices.DirectorySearcher 
  $Searcher.PageSize = 100 
  $Searcher.SearchScope = "subtree" 
  $Searcher.Filter = "(&amp;(objectCategory=$objectCategory)(objectClass=$objectClass))"
  $Searcher.PropertiesToLoad.Add("distinguishedName")|Out-Null
  $Searcher.PropertiesToLoad.Add("modifyTimeStamp")|Out-Null
  
  Function modiScan{
  
      forEach ($users In $userObjects) 
      { 
  
              $DN = $users.Properties.Item("distinguishedName")[0]
              $dnarr.add($DN)|Out-Null
      }
      #$dnarr
      foreach($dnn in $dnarr){
          $error = $false
          $lastmd = New-Object System.Collections.ArrayList
          ForEach ($DC In $Domain.DomainControllers){                    
              $Server = $DC.Name
              $Base = "LDAP://$Server/"+$dnn
              $Searcher.SearchRoot = $Base                    
              try{
                  $Results2 = $Searcher.FindAll()
                  ForEach ($Result2 In $Results2) 
                  {   
                      $DN2 = $Result2.Properties.Item("distinguishedName")[0]
                      if($DN2 -eq $dnn){
                          $modi = $Result2.Properties.Item("modifyTimeStamp")[0]
                          if($modi){
                              $lastmd.Add($modi)|Out-Null                            
                          }
                      } 
                  }
              }
              catch{
                  $error = $true
              }                    
          }
          if($error -eq $true){
              $lastModi = "None-set"
              $global:noneModi++
          }
          else{
            $lastModi = ($lastmd |measure -max).maximum      
            if($lastModi -ne $null){   
                  $lastModi = $lastModi.ToString("yyyy/MM/dd")
                  if($lastModi.split("/")[0] -eq 2015){
                       $global:modi2015++
                  }       
                  elseif($lastModi.split("/")[0] -eq 2016){
                       $global:modi2016++
                  }
                  elseif($lastModi.split("/")[0] -eq 2017){
                       $global:modi2017++
                  }
                   else{
                       $global:otherModi++
                  }
              }
              else{
                  $lastModi = "N/A"
                  $global:noneModi++
              }
          }
          #$lastModi
        $obj = New-Object -TypeName psobject
        $obj | Add-Member -MemberType NoteProperty -Name "modi" -Value $lastModi      
        $obj | Export-Csv -Path "$outFileModi" -NoTypeInformation -append -Delimiter $Delimiter
  
     }
  }
  modiScan
  </code></pre>