You didn't show any code so I assume you're using DirectorySearcher
private DirectorySearcher _Searcher;
_Searcher = new DirectorySearcher();
_Searcher.CacheResults = false;
// Set the rest of the properties
This disables local caching. If you have to repeat the query, the data is fetched from the DC again and this may slow the performance and causes more traffic in your LAN.
HTH
Teme64
Veteran Poster
1,031 posts since Aug 2008
Reputation Points: 218
Solved Threads: 203
What if you have a following group in your domain:
'kitchen.to.sinks.sales.department.houston@company.intra'
Ok, I think you'd be better to search with a filter instead of any nested loops. With VBScript it would go something like this
Option Explicit
Dim adoCommand, adoConnection, strBase, strFilter, strAttributes
Dim objRootDSE, strDNSDomain, strQuery, adoRecordset, strFirst, strLast
' Setup ADO objects.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection
' Search entire Active Directory domain.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"
' Filter on all users
strFilter = "(&(objectCategory=person)(objectClass=user))"
' Comma delimited list of attribute values to retrieve.
strAttributes = "firstName,lastName"
' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False
' Run the query.
Set adoRecordset = adoCommand.Execute
' Enumerate the resulting recordset.
Do Until adoRecordset.EOF
' Retrieve values.
strFirst = adoRecordset.Fields("firstName").Value
strLast = adoRecordset.Fields("lastName").Value
Wscript.Echo strFirst & " " & strLast
' Move to the next record in the recordset.
adoRecordset.MoveNext
Loop
' Clean up.
adoRecordset.Close
adoConnection.Close
and "translated" to C#
DirectorySearcher oSearcher;
oSearcher = new DirectorySearcher();
oSearcher.SearchRoot = new DirectoryEntry("LDAP://CN=users,DC=yourdomain,DC=com");
oSearcher.CacheResults = false;
oSearcher.ClientTimeout=new TimeSpan(0,0,30); // 30 second for timeout
oSearcher.PageSize = 100;
oSearcher.Tombstone = false; // Do not include deleted items
oSearcher.Filter = "(&(objectCategory=person)(objectClass=user)(firstName=*)(lastName=*)"; // Users only
// Query
SearchResultCollection collNames = oSearcher.FindAll();
{
// Handle results
}
// Some error handling too!
Didn't test the code above myself (as one always should), but it should work. Just in case, there's a good documentation of the DirectorySearcher Class in MSDN.
HTH
Teme64
Veteran Poster
1,031 posts since Aug 2008
Reputation Points: 218
Solved Threads: 203