简体   繁体   English

C#在Active Directory中创建OU

[英]C# Create OU in Active Directory

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 =内部,DC = net,DC =内部,DC = net'导致错误,因为域显然不存在。

My question is can I create an OU using strPath without .Children.Add ? 我的问题是我可以使用没有.Children.Add strPath创建OU吗?

I'm not familiar with AD and this is something I inherited from the guy before me. 我不熟悉AD,这是我从我之前的那个人那里继承的。

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

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 =某事”)。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#Active Directory,登录特定OU - C# Active Directory, Login in a specific OU C#和Active Directory:测试OU是否存在 - C# and Active Directory : test if an OU exist 尝试从 Active Directory 检索和 OU,然后使用 C# 中的目录条目在 OU 上设置新属性 - Trying to Retrieve and OU from Active Directory, then set new properties on an OU using a Directory Entry in C# 使用具有层次结构的c#从活动目录获取OU列表 - Get OU list from active directory using c# with hierarchy 使用C#在Active Directory中获取用户的父OU - Get parent OU of user in Active Directory using C# 如何在asp.net c#Web窗体应用程序的Active Directory OU中创建所有电子邮件地址的数组列表? - How to create an arraylist of all email addresses in an Active Directory OU in an asp.net c# web forms application? 在活动目录中为不同域创建OU - Create OU in active directory for different domain 当我们在asp.net c#中的活动目录中创建用户时,如何在活动目录中嵌套OU时,如何给出路径来识别OU? - How to give path to identify OU when we have nested OU's in active directory while creating a user in active directory in asp.net c#? 尝试在Active Directory C#中委派OU的控件时,出现DirectoryServicesCOMException“发生操作错误” - DirectoryServicesCOMException “operations error occurred” when trying to delegate control for OU in Active directory C# c#Active Directory…在根目录下创建OU时很奇怪 - c# Active Directory… weirdness when creating a OU just under the root
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM