I have written a console application that generates reports on accounts in Active Directory and Novell e-Directory trees that should be deleted. This program works great for generating a very informative list as it pertains to my company's requirements.
I have now been asked to see If I can enhance this program to additionally delete certain accounts.
I am only using Directory.Services for the connections to the different trees and don't want to change this connection type. Now I can and have successfully deleted objects that are located at the root of my search. My problem now is I cannot seem to delete any userobjects found in a subOU.
Here is the code I have for my function that deletes User Objects...
static void Perform_Deletions(List<UserAccountObject> User_List, DirectoryEntry myLdapConnection)
{
DirectoryEntry userToDelete;
myLdapConnection.RefreshCache();
string cnRegex = @"^([^,]+)";
Regex myCNRegex = new Regex(cnRegex, RegexOptions.IgnoreCase);
foreach(UserAccountObject user in User_List)
{
foreach(Match myMatch in myCNRegex.Matches(user.Distinguished_Name))
{
string cn = myMatch.ToString();
userToDelete = myLdapConnection.Children.Find(cn);
myLdapConnection.Children.Remove(userToDelete);
myLdapConnection.CommitChanges();
}
}
}
I did remove a the error checking and renamed some of the areas so as to not give out internal information. But anyhow. I am sure that my problem is probably with the 10th line of this code. How do I modify this line or change this function around so that if the initial DirectoryEntry is pointed to "LDAP://server1.contoso.com/OU=users,DC=contoso,DC=com" ; and the user object is in "OU=Team1,OU=users,DC=contoso,DC=com" it will too be deleted?
Currently with this code all users in the original entry will be deleted in either AD or e-Directory.
Much thanks in advance for all the help!