2016-04-14 50 views
0

我已经编写了一个控制台应用程序,用于生成应当删除的Active Directory和Novell电子目录树中的帐户报告。该程序非常适合于生成非常丰富的列表,因为它与我公司的要求相关。以编程方式删除具有目录服务的用户

我现在被要求看看如果我可以增强这个程序来额外删除某些帐户。

我只使用Directory.Services连接到不同的树,并且不想更改此连接类型。现在我可以并已成功删除位于搜索根目录的对象。我现在的问题是,我似乎无法删除子单元中找到的任何有用物体。

下面是代码我有我的函数删除用户对象...

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(); 
     } 
    } 
} 

我没有删除的错误检查和重新命名的一些领域,从而给不给出来的内部信息。但无论如何。我相信我的问题可能在于此代码的第10行。如何修改此行或更改此功能,以便如果初始DirectoryEntry指向“LDAP://server1.contoso.com/OU=users,DC=contoso,DC=com”;并且用户对象位于“OU = Team1,OU = users,DC = contoso,DC = com”中,它也将被删除?

当前使用此代码原始条目中的所有用户将被删除在AD或电子目录中。

非常感谢所有的帮助!

回答

0

所以我能够编写一个适合我的需求的解决方案,但我觉得这可能不是最好的解决方案,因为我必须为每个需要删除的DN创建和销毁与目录服务器的连接。必须有一种方法可以仅通过单个连接发送要删除的DN列表。

static void Perform_Deletions(List<UserAccountObject> User_List, string directory) 
    { 
     string ldapServer = null; 
     string parentOU = null; 
     string userCN = null; 
     string ldapDirectory = null; 
     string userName = null; 
     string passWord = null; 

     // REGEX value to only return OU path portion of User DN 
     string dnSuffixRegex = @"ou.*"; 
     Regex myDNRegex = new Regex(dnSuffixRegex, RegexOptions.IgnoreCase); 

     // REGEX to only Return the CN portion of User DN 
     string cnRegex = @"^([^,]+)"; 
     Regex myCNRegex = new Regex(cnRegex, RegexOptions.IgnoreCase); 

     switch (directory) 
     { 
      case "AD1": 
       { 
        ldapDirectory = "LDAP://ad1.contosoe.com/"; 
        userName = "Admin"; 
        passWord = @"[email protected]$$W0rd1"; 

        break; 
       } 
      case "AD2": 
       { 
        ldapDirectory = "LDAP://ad2.contosof.com/"; 
        userName = "Admin"; 
        passWord = @"[email protected]$$W0rd1"; 

        break; 
       } 
      case "EDIR1": 
       { 
        ldapDirectory = "LDAP://edirectory1.contosoc.com/"; 
        userName = @"cn=Admin,o=Root"; 
        passWord = @"[email protected]$$W0rd1"; 

        break; 
       } 
      case "AD3": 
       { 
        ldapDirectory = "LDAP://ad3.contosod.com/"; 
        userName = "Admin"; 
        passWord = @"[email protected]$$W0rd1"; 

        break; 
       } 
      case "EDIR2": 
       { 
        ldapDirectory = "LDAP://edirectory2.contosob.com/"; 
        userName = @"cn=Admin,o=Root"; 
        passWord = @"[email protected]$$W0rd1"; 

        break; 
       } 
      case "EDIR3": 
       { 
        ldapDirectory = "LDAP://edirectory3.contosoa.com/"; 
        userName = @"cn=Admin,o=Root"; 
        passWord = @"[email protected]$$W0rd1"; 

        break; 
       } 
      default: 
       { 
        break; 
       } 
     } 

     foreach (UserAccountObject user in User_List) 
     { 
      foreach (Match cnMatch in myCNRegex.Matches(user.Distinguished_Name)) 
      { 
       userCN = cnMatch.ToString(); 
      } 

      foreach (Match dnMatch in myDNRegex.Matches(user.Distinguished_Name)) 
      { 
       parentOU = dnMatch.ToString(); 
      } 

      ldapServer = ldapDirectory + parentOU; 

      try 
      { 
       DirectoryEntry myLdapconnection = new DirectoryEntry(ldapServer, userName, passWord, AuthenticationTypes.ServerBind); 
       DirectoryEntry userToDelete = myLdapconnection.Children.Find(userCN); 
       myLdapconnection.RefreshCache(); 
       myLdapconnection.Children.Remove(userToDelete); 
       myLdapconnection.CommitChanges(); 
       myLdapconnection.Close(); 
       myLdapconnection.Dispose(); 
       user.Deletion_Status = "SUCCEEDED"; 
      } 
      catch (Exception e) 
      { 
       user.Deletion_Status = "FAILED"; 
       Console.WriteLine("Exception Caught:\n\n{0}", e.ToString()); 
      } 
     } 
    } 
相关问题