title: Obtain accounts from all ServiceAccounts OU's across child-domains
body: <p>I've been tasked with providing a list of all service accounts across multiple domains, but wither only receive accounts from the primary domain or get an "a referral was returned from the server"</p>
  
  <p>If I run the code below, it returns the service accounts from the primary domain, but not the child domains.  If I edit the code and manually put in one of the child domains, it will return "a referral was returned from the server" error.  I've tried using a global catalog server and port (3268) as the server but still get the "referral" error.  I'm relatively new to PowerShell, so I apologize up front if I've left out any information you much need.</p>
  
  <pre><code>$domains = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest() | Select Domains
  
  ForEach($domain in $domains.Domains){
      $ous = (Get-ADOrganizationalUnit -Filter "Name -like 'ServiceAccounts'" -Server ($domain.Name) -SearchScope Subtree)    
  }
  
  $ous | ForEach-Object{Get-ADUser -Filter * -SearchBase $_.DistinguishedName} | Select SamAccountName | Export-Csv c:\temp\service.csv
  </code></pre>
  
  <p>Basically I need to find all OU's named "ServiceAccounts" and export the users of each to a csv file, from both the primary domain and child domains. Please help, thanks!</p>