2008-12-30 36 views
5

Tôi đang cố gắng tạo một OU cho Active Directory bằng cách sử dụng mã bên dưới.C# Tạo OU trong Active Directory

strPath = "OU=TestOU,DC=Internal,DC=Com" 

DirectoryEntry objOU; 
objOU = ADentry.Children.Add(strPath, "OrganizationalUnit"); 
objOU.CommitChanges(); 

Vấn đề là strPath chứa đường dẫn đầy đủ 'OU = TestOU, DC = Internal, DC = net' để sử dụng .Children.Add đang thực hiện con đường ldap 'OU = TestOU, DC = Internal, DC = net, DC = Internal, DC = net 'dẫn đến lỗi do miền rõ ràng không tồn tại.

Câu hỏi của tôi là tôi có thể tạo OU bằng cách sử dụng strPath mà không cần .Children.Add?

Tôi không quen với AD và đây là điều tôi thừa hưởng từ anh chàng trước tôi.

Trả lời

12

thử này

using System; 
using System.DirectoryServices; 

namespace ADAM_Examples 
{ 
    class CreateOU 
    { 
     /// <summary> 
     /// Create AD LDS Organizational Unit. 
     /// </summary> 
     [STAThread] 
     static void Main() 
     { 
      DirectoryEntry objADAM; // Binding object. 
      DirectoryEntry objOU; // Organizational unit. 
      string strDescription; // Description of OU. 
      string strOU;   // Organiztional unit. 
      string strPath;   // Binding path. 
     // Construct the binding string. 
     strPath = "LDAP://localhost:389/O=Fabrikam,C=US"; 

     Console.WriteLine("Bind to: {0}", strPath); 

     // Get AD LDS object. 
     try 
     { 
      objADAM = new DirectoryEntry(strPath); 
      objADAM.RefreshCache(); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine("Error: Bind failed."); 
      Console.WriteLine("   {0}", e.Message); 
      return; 
     } 

     // Specify Organizational Unit. 
     strOU = "OU=TestOU"; 
     strDescription = "AD LDS Test Organizational Unit"; 
     Console.WriteLine("Create: {0}", strOU); 

     // Create Organizational Unit. 
     try 
     { 
      objOU = objADAM.Children.Add(strOU, 
       "OrganizationalUnit"); 
      objOU.Properties["description"].Add(strDescription); 
      objOU.CommitChanges(); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine("Error: Create failed."); 
      Console.WriteLine("   {0}", e.Message); 
      return; 
     } 

     // Output Organizational Unit attributes. 
     Console.WriteLine("Success: Create succeeded."); 
     Console.WriteLine("Name: {0}", objOU.Name); 
     Console.WriteLine("   {0}", 
      objOU.Properties["description"].Value); 
     return; 
    } 
} 
} 
4

Cách duy nhất để tạo ra một đối tượng với System.DirectoryServices là để tạo ra một đối tượng DirectoryEntry cho phụ huynh và sử dụng DirectoryEntry.Children.Add.

Tôi nghĩ di chuyển tốt nhất của bạn vào thời điểm này là sử dụng đường dẫn bạn có và trích xuất phần bạn cần ("OU = cái gì đó").

1

Không, bạn không thể. Nhưng bạn có một số sai lầm trong mã của bạn, hãy thử điều này:

string rootOU = @"LDAP://DC=Internal,DC=Com/OU=Root OU,DC=Internal,DC=Com; // or simply "DC=Internal,DC=Com" instead of "OU=Root OU,DC=Internal,DC=Com" if you want to create your test OU in root 
DirectoryEntry objAD = new DirectoryEntry(rootOU, userName, password); 
DirectoryEntry objOU = objAD.Children.Add("OU=Test OU", "OrganizationalUnit"); 
objOU.CommitChanges(); 
Các vấn đề liên quan