2008-12-30 39 views
5

Tengo dificultades para crear una unidad organizativa para Active Directory utilizando el siguiente código.C# Crear unidad organizativa en Active Directory

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

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

El problema es strPath contiene la ruta completa 'OU = TestOU, DC = Interna, DC = net' por lo que usar .Children.Add está haciendo la ruta LDAP 'OU = TestOU, DC = Interna, DC = net, DC = Internal, DC = net 'que da como resultado un error ya que el dominio obviamente no existe.

Mi pregunta es ¿puedo crear una unidad organizativa utilizando strPath sin .Children.Add?

No estoy familiarizado con AD y esto es algo que heredé del hombre antes que yo.

Respuesta

12

probar esto

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

La única manera de crear un objeto con System.DirectoryServices es crear un objeto DirectoryEntry al padre y utilizar DirectoryEntry.Children.Add.

Creo que su mejor jugada en este momento es utilizar la ruta que tiene y extraer la pieza que necesita ("OU = algo").

1

No, no puedes. Pero tiene algunos errores en su código, intente esto:

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(); 
Cuestiones relacionadas