Fast search by LinkedMasterAccount RRS feed

  • Question

  • Hello,

    I need to find if UserDomain\User has linked mailbox in the MailDomain domain. I wrote a query


    $SearchString = "UserDomain\User"

    $AllMailboxes  = Get-mailbox -results unlimited -Filter ”RecipientTypeDetails -eq 'LinkedMailbox'” -domaincontroller $DomainController | where {$_.LinkedMasterAccount -eq $SearchString}

    The problem is that script has to check 100K+ mailboxes, and query runs very slow. Is there the faster way? I know if script will try to assign LinkedMasterAccount, error message, or acceptance will apear almost instantly.

    Thank you

    Saturday, May 5, 2012 12:26 AM


  • The best way is probably to use ADSI and do a direct search via LDAP eg if you grab the SID of the account you want to search for before you start and do a search on for any objects that have the msExchMasterAccountSID set to the SID of account your searching for eg 

    $objUser =  ((New-Object System.Security.Principal.NTAccount(("Contoso\Claire"))).Translate([System.Security.Principal.SecurityIdentifier]))
    [byte[]]$userBA = ,0 * $objUser.BinaryLength
    $sb = New-Object -TypeName System.Text.StringBuilder                        
    for ($i=0; $i -lt $userBA.Length; $i++){
      $sb.AppendFormat("\{0}",  $userBA[$i].ToString("X2")) | Out-Null
    $strsid = $sb.ToString() 
    $root = [ADSI]''
    $searcher = new-object System.DirectoryServices.DirectorySearcher($root)
    $searcher.filter = "(msExchMasterAccountSID=" + $strsid + ")"
    $user = $searcher.findall()

    • Edited by Glen ScalesMVP Monday, May 7, 2012 6:43 AM
    • Marked as answer by WictorL Tuesday, May 8, 2012 11:16 AM
    Monday, May 7, 2012 6:42 AM