Get All Active directory domains in a Network .
ArrayList Doamin = new ArrayList();SPSecurity.RunWithElevatedPrivileges(delegate
{
Forest currentForest = Forest.GetCurrentForest();
DomainCollection domains = currentForest.Domains;
foreach (Domain objDomain in domains)
{
Doamin.Add(objDomain.Name);
}
});
===========================see more code==============================
All Enabled users from AD.
public static void GetAllInactivePersonsNEm(){
SPSecurity.RunWithElevatedPrivileges(delegate
{
//StreamWriter SW;
//SW = File.AppendText("C:\\ADall_InactivePrsns_NEm.csv");
//SW.WriteLine("User Name, Email , Domain Directory");
Forest currentForest = Forest.GetCurrentForest();
DomainCollection domains = currentForest.Domains;
try
{
foreach (Domain objDomain in domains)
{
Console.WriteLine("---Domain--" + objDomain.Name + "------");
string lDAPName = "LDAP://" + objDomain.Name;
DirectoryEntry entry = new DirectoryEntry("LDAP://" + objDomain.Name);
DirectorySearcher Dsearch = new DirectorySearcher(entry);
Dsearch.Filter = "(&(objectClass=user)(objectCategory=Person)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))";
try
{
foreach (SearchResult sResultSet in Dsearch.FindAll())
{
DirectoryEntry de = sResultSet.GetDirectoryEntry();
Console.WriteLine(GetProperty(sResultSet, "cn"));
//String ObjEmail = (GetProperty(sResultSet, "mail"));
//if (!String.IsNullOrEmpty(ObjEmail))
//{
// SW.WriteLine(GetProperty(sResultSet, "cn").Replace(",", " ") + "," + GetProperty(sResultSet, "mail") + "," + entry.Path.ToString());
//}
}
}
catch (Exception ex)
{
}
}
}
catch (Exception ex)
{
}
// SW.Close();
Console.WriteLine("AD aAtive Document Write Completed..4..");
Console.ReadLine();
});
}
public static string GetProperty(SearchResult searchResult, string PropertyName)
{
if (searchResult.Properties.Contains(PropertyName))
{
return searchResult.Properties[PropertyName][0].ToString();
}
else
{
return string.Empty;
}
}
No comments:
Post a Comment