2016-04-14 75 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()); 
      } 
     } 
    }