I modified your previous code so that it now enumerates all disabled accounts and load their manager's properties. I have not done this in production, so I am not sure if this is the best way to find manager's properties; but it works
public static DirectoryEntry GetDirectoryEntry()
{
DirectoryEntry de = new DirectoryEntry();
de.Path = "LDAP://" your path here;
de.AuthenticationType = AuthenticationTypes.Secure;
return de;
}
public static void GetDisabledAccountsAndTheirManagers()
{
DirectoryEntry entry = GetDirectoryEntry();
try
{
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(&(objectCategory=User)(userAccountControl:1.2.840.113556.1.4.803:=2))";
search.PropertiesToLoad.Add("displayName");
search.PropertiesToLoad.Add("Manager");
SearchResultCollection resultCollection = search.FindAll();
foreach (SearchResult result in resultCollection )
{
if (result.Properties.Contains("displayName"))
{
Console.WriteLine(result.Properties["displayName"][0]);
}
if (result.Properties.Contains("Manager"))
{
Console.WriteLine(result.Properties["Manager"][0]);
DirectoryEntry managerDirEntry = new DirectoryEntry("LDAP://" + result.Properties["Manager"][0]);
if (managerDirEntry != null)
{
if (managerDirEntry.Properties.Contains("displayName"))
{
Console.Write("Manager's Diplay Name: " + managerDirEntry.Properties["displayName"][0].ToString());
}
if(managerDirEntry.Properties.Contains("email"))
{
Console.Write("Manager's email: " + managerDirEntry.Properties["email"][0].ToString());
}
Console.WriteLine();
}
}
}
}
catch (Exception ex)
{
string debug = ex.Message;
throw ex;
}
}