Я изо всех сил пытаюсь создать OU для Active Directory, используя приведенный ниже код.
strPath = "OU=TestOU,DC=Internal,DC=Com"
DirectoryEntry objOU;
objOU = ADentry.Children.Add(strPath, "OrganizationalUnit");
objOU.CommitChanges();
Проблема в том, что strPath содержит полный путь 'OU = TestOU, DC = Internal, DC = net', поэтому использование .Children.Add делает путь ldap 'OU = TestOU, DC = Internal, DC = net, DC = Internal, DC = net ', что приводит к ошибке, поскольку домен явно не существует.
У меня вопрос: могу ли я создать OU с использованием strPath без .Children.Add?
Я не знаком с AD, и это то, что я унаследовал от парня до меня.





попробуй это
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;
}
}
}
Единственный способ создать объект с помощью System.DirectoryServices - создать объект DirectoryEntry для родительского объекта и использовать DirectoryEntry.Children.Add.
Я думаю, что ваш лучший ход на данном этапе - использовать имеющийся у вас путь и извлечь нужную часть («OU = something»).
Нет, не можешь. Но в вашем коде есть ошибки, попробуйте следующее:
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();