C# 在 Active Directory 中创建 OU

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/400691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 02:01:31  来源:igfitidea点击:

C# Create OU in Active Directory

c#active-directory

提问by

I'm struggling to create an OU for Active Directory using the code below.

我正在努力使用下面的代码为 Active Directory 创建一个 OU。

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

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

The problem is strPath contains the full path 'OU=TestOU,DC=Internal,DC=net' so using .Children.Add is making the ldap path 'OU=TestOU,DC=Internal,DC=net,DC=Internal,DC=net' which results in an error as the domain obviously doesn't exist.

问题是 strPath 包含完整路径 'OU=TestOU,DC=Internal,DC=net' 所以使用 .Children.Add 正在制作 ldap 路径 'OU=TestOU,DC=Internal,DC=net,DC=Internal,DC =net' 这会导致错误,因为域显然不存在。

My question is can I create an OU using strPathwithout .Children.Add?

我的问题是我可以使用strPath不创建 OU.Children.Add吗?

I'm not familiar with AD and this is something I inherited from the guy before me.

我不熟悉 AD,这是我从我之前的那个人那里继承的东西。

回答by

try this

尝试这个

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

回答by Steve Evans

The only way to create an object with System.DirectoryServices is to create a DirectoryEntry object to the parent and use DirectoryEntry.Children.Add.

使用 System.DirectoryServices 创建对象的唯一方法是为父级创建 DirectoryEntry 对象并使用 DirectoryEntry.Children.Add。

I think your best move at this point is to use the path you have and extract the part you need ("OU=something").

我认为此时您最好的做法是使用您拥有的路径并提取您需要的部分(“OU=something”)。

回答by thezar

No, you can't. But you have some mistakes in you code, try this:

不,你不能。但是你的代码有一些错误,试试这个:

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