I am currently working on a program that pulls all of the users in our organization and lists their samaccountname, displayname, etc. We have approximately 55000 users; however, everytime I run the query on the whole organization, at some point I get the error "Object on server not found." Sometimes the query will run in its entirety, sometimes it will fail about 5000 in, sometimes it will fail 40000 in. I can't figure out why or how to stop it. Any suggestions? I'm a novice programmer and am stumped. I have spent hours searching forums and websites. Please see code below:
private static string _path = "LDAP://DC=DOMAIN,DC=net";private static string _serviceAccountName = @"domain\userid";private static string _servicePassword = "********";private DirectoryEntry entry = new DirectoryEntry();private string filterString = "(&(mailnickname=*)(msExchHomeServerName=*))";
#region GetMSXUsers public void GetMSXUsers() { int i = 0; //to keep track of arrays int j = 0; //to keep track of deprov array string serverParse = ""; string dn = "";
try { string homemdb="";
entry.Path = _path; entry.Username = _serviceAccountName; entry.Password = _servicePassword;
DirectorySearcher mySearcher = new DirectorySearcher(entry); mySearcher.Filter = filterString.ToString(); mySearcher.PageSize = 1000; mySearcher.PropertyNamesOnly = true; //Add all properties that need to be fetched mySearcher.PropertiesToLoad.Add("samAccountName"); ; mySearcher.PropertiesToLoad.Add("displayName"); mySearcher.PropertiesToLoad.Add("ITBillcc"); mySearcher.PropertiesToLoad.Add("mDBOverQuotaLimit"); mySearcher.PropertiesToLoad.Add("homemdb"); mySearcher.PropertiesToLoad.Add("msExchHomeServerName"); //The search scope specifies how deep the search needs to be, it can be either "base"- which means only in the current //level, and "OneLevel" which means the base and one level below and then "subtree"-which means the entire tree needs //to be searched.
mySearcher.SearchScope = SearchScope.Subtree; SearchResultCollection resultUsers = mySearcher.FindAll(); int temp = 0; temp = resultUsers.Count; temp=0;
foreach (SearchResult srUser in resultUsers) //get LDAP query results { DirectoryEntry de = srUser.GetDirectoryEntry(); dn = de.Properties["distinguishedName"][0].ToString();
if (dn.IndexOf("DE-PROV")!= -1) { DeProv[j,0] = de.Properties["samAccountName"][0].ToString().ToLower(); DeProv[j,1] = de.Properties["displayName"][0].ToString().ToLower(); j++; deProvCnt++; } else if ((dn.IndexOf("DE-PROV")== -1) && (dn.IndexOf("SystemMailbox")== -1)) { ADUser[i,0] = de.Properties["samAccountName"][0].ToString().ToLower(); ADUser[i,1] = de.Properties["displayName"][0].ToString(); if (de.Properties.Contains("ITBillcc")) { ADUser[i,2] = de.Properties["ITBillcc"][0].ToString(); } else { ADUser[i,2] = "NONE LISTED"; } if (de.Properties.Contains("mDBOverQuotaLimit")) { ADUser[i,3] = de.Properties["mDBOverQuotaLimit"][0].ToString(); } else { homemdb = de.Properties["homeMDB"][0].ToString().ToLower();
if ((homemdb.IndexOf("400")!= -1) || (homemdb.IndexOf("3 (do not use-jaexmb2-it")!= -1)) { ADUser[i,3]="375000"; } else if ((homemdb.IndexOf("unrestricted")!= -1) || (homemdb.IndexOf("(hoexmbx1")!= -1)) { ADUser[i,3] = "1000000"; } else { ADUser[i,3] = "125000"; } }
if (de.Properties.Contains("msExchHomeServerName")) { int index=0; serverParse = de.Properties["msExchHomeServerName"][0].ToString().ToLower(); index = serverParse.IndexOf("/cn=servers/cn="); index = index + 15; ADUser[i,5] = serverParse.Substring(index).ToUpper(); } else { ADUser[i,5] = "NONE LISTED"; }
i = i + 1; userCount= userCount + 1; } temp++; de.Close();
}
mySearcher.Dispose(); entry.Close();
} catch (Exception e) //catch connection error { string errorMessage = "Message: " + e.Message; metricsLog.Log((int)LL.Min, "GetUsers", "1", "Error", errorMessage, dn, "", "F", "", "", ""); return; } } #endregion