I have an application that needs to gather the LDAP attribute "badPwdCount" from every DC in my domain. This is my code:
DataTable table = new DataTable();
table.Columns.Add("DC Name", typeof(string));
table.Columns.Add("Site", typeof(string));
table.Columns.Add("Bad Pwd Count", typeof(int));
DirectoryEntry dcs = new DirectoryEntry("LDAP://OU=Domain Controllers,DC=****,DC=com");
DirectorySearcher searcher = new DirectorySearcher("sAMAccountName=" + WCTB_samname.Text);
DirectoryEntry user = searcher.FindOne().GetDirectoryEntry();
foreach (DirectoryEntry dc in dcs.Children)
{
DataRow row = table.NewRow();
row["DC Name"] = dc.Properties["cn"].Value.ToString();
dc.RefreshCache(new string[] { "msDS-SiteName" });
row["Site"] = dc.Properties["msDS-SiteName"].Value.ToString();
//new directory entry with the DC hardcoded in the LDAP path
DirectoryEntry user_on_dc = new DirectoryEntry(user.Path.Replace("LDAP://","LDAP://"+row["DC Name"]+"/"));
//this is the part that takes a long long time.
row["Bad Pwd Count"] = user_on_dc.Properties.Contains("badPwdCount") ? (int)user_on_dc.Properties["badPwdCount"].Value : 0;
table.Rows.Add(row);
}
WCGV_dclist.DataSource = table;
WCGV_dclist.DataBind();
A little background, "badPwdCount" is a non replicated attribute, so it will be something different on every DC. This is why I need to go to every DC one at a time a pluck the information. It takes forever to do this, anyone have any tips to make this
faster? Perhaps there is a multi-threadable option?
Jordo C
Member
18 Points
18 Posts
Getting "badPwdCount" From every DC Faster?
Jan 23, 2012 11:48 AM|LINK
Hello,
I have an application that needs to gather the LDAP attribute "badPwdCount" from every DC in my domain. This is my code:
DataTable table = new DataTable(); table.Columns.Add("DC Name", typeof(string)); table.Columns.Add("Site", typeof(string)); table.Columns.Add("Bad Pwd Count", typeof(int)); DirectoryEntry dcs = new DirectoryEntry("LDAP://OU=Domain Controllers,DC=****,DC=com"); DirectorySearcher searcher = new DirectorySearcher("sAMAccountName=" + WCTB_samname.Text); DirectoryEntry user = searcher.FindOne().GetDirectoryEntry(); foreach (DirectoryEntry dc in dcs.Children) { DataRow row = table.NewRow(); row["DC Name"] = dc.Properties["cn"].Value.ToString(); dc.RefreshCache(new string[] { "msDS-SiteName" }); row["Site"] = dc.Properties["msDS-SiteName"].Value.ToString(); //new directory entry with the DC hardcoded in the LDAP path DirectoryEntry user_on_dc = new DirectoryEntry(user.Path.Replace("LDAP://","LDAP://"+row["DC Name"]+"/")); //this is the part that takes a long long time. row["Bad Pwd Count"] = user_on_dc.Properties.Contains("badPwdCount") ? (int)user_on_dc.Properties["badPwdCount"].Value : 0; table.Rows.Add(row); } WCGV_dclist.DataSource = table; WCGV_dclist.DataBind();A little background, "badPwdCount" is a non replicated attribute, so it will be something different on every DC. This is why I need to go to every DC one at a time a pluck the information. It takes forever to do this, anyone have any tips to make this faster? Perhaps there is a multi-threadable option?
Thanks!
JJ