Hello,
i have one script, what retrieve mail contacts thogh new-pssession to exchange server and invoke get-contact, like this:
$ExchangeSession =New-PSSession-ConfigurationName"Microsoft.Exchange"-ConnectionUri"http://$ExchangeServerName/PowerShell/"-AuthenticationKerberos
$MailContactList = Invoke-Command -Session $ExchangeSession -ScriptBlock { param($OUName,$domainController) Get-MailContact -OrganizationalUnit $OUName -DomainController $DomainController -ErrorAction stop } -ArgumentList $OUName,$DomainController
time of execution 0,9-1,1 sec.
I need speed it up. I know that Exchange server get information about contacts from Active Directory. And i tried get information directly from AD.
i look 3 ways:
1) Get-ADObject, like
$ADContacts = Get-ADObject -SearchBase $OUName -LDAPFilter "(&(objectClass=contact)(msExchRecipientDisplayType=6))" -Server $DomainController
time of execution 3 sec.
2) ADSI, like
$OUobj = [adsi]"LDAP://$domainController/$OUName"
$OUobj.Children.SchemaFilter.Add("contact")
$Contacts = [array]$OUobj.Children
time execution 2,4-2,6 sec.
3) DirectorySeacher, like
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.PageSize = 1000
$objSearcher.Filter = "(&(objectClass=Contact)(msExchRecipientDisplayType=6))"
$objSearcher.SearchScope = "Subtree"
$objSearcher.SearchRoot = "LDAP://$DomainController/$OUName"
$colResults = $objSearcher.FindAll()
return $colResults
time execution 2,5-2,6 second.
All times are time of first query. Second same query performed 0,1 sec. As i understand second query get result from local cache, not from domain controller.
Why query information through Exchange faster that direct request to domain controller?
Any ideas? Maybe another way to execute query?