C#通过数组循环以创建Active Directory OU

C#通过数组循环以创建Active Directory OU,c#,active-directory,C#,Active Directory,我正在尝试在Active Directory中创建嵌套的组织单位,我很快就能让它工作了。下面我有一个问题需要帮助。我正在检查OU是否存在,如果不存在,我需要创建它,strOUArray包含OU=Test OU=Test2和OU=Test3。我需要先创建OU=Test的代码,然后将其用作下面//问题行上的父OU,从而允许在OU=Test中创建下一个OU=Test2。目前,在下面的代码中,所有OU都将在根目录中创建,因为我不知道如何使用//问题行中创建的第一个OU。我试过使用: parent = n

我正在尝试在Active Directory中创建嵌套的组织单位,我很快就能让它工作了。下面我有一个问题需要帮助。我正在检查OU是否存在,如果不存在,我需要创建它,strOUArray包含OU=Test OU=Test2和OU=Test3。我需要先创建OU=Test的代码,然后将其用作下面//问题行上的父OU,从而允许在OU=Test中创建下一个OU=Test2。目前,在下面的代码中,所有OU都将在根目录中创建,因为我不知道如何使用//问题行中创建的第一个OU。我试过使用:

parent = new DirectoryEntry("LDAP://" + strOUArray[x-1] + "," + dcSubString); //note x-1
此操作失败,因为要在中创建的第一个OU的父级不存在。非常感谢您的帮助,我的最后期限很紧,只需要离开这里,谢谢您的帮助

String strOUs = container.Substring(0, container.IndexOf(@",DC="));
int dcIndex = container.IndexOf("DC=");
string dcSubString = container.Substring(dcIndex); //dcSubString = DC=Internal,DC=Net
string[] strOUArray = strOUs.Split(new Char[] { ',' });

for (int x = 0; x < strOUArray.Length; x++)
{
if (DirectoryEntry.Exists("LDAP://" + strOUArray[x] + "," + dcSubString))
 {

 }
else
 {
 DirectoryEntry objOU;
 DirectoryEntry parent = new DirectoryEntry("LDAP://" + dcSubString); //PROBLEM LINE

 objOU = parent.Children.Add(strOUArray[x], "OrganizationalUnit");
 objOU.CommitChanges();
 }
}
String strOUs=container.Substring(0,container.IndexOf(@),DC=“);
int dcIndex=container.IndexOf(“DC=”);
字符串dcSubString=container.Substring(dcIndex)//dcSubString=DC=Internal,DC=Net
字符串[]strOUArray=strOUs.Split(新字符[]{',});
对于(int x=0;x
似乎您的
//问题行
strOUArray[x]
从字符串连接中删除。发帖时是不是打错了

还有这个片段:

DirectoryEntry parent = new DirectoryEntry();
parent = new DirectoryEntry("...");

您正在创建一个
目录条目
,然后立即在下一行中丢弃对它的引用。

好吧,我想,我会尝试这样做:

  • 首先绑定到顶级(即LDAP://
  • 创建第一个OU
  • 然后递归地
    • 绑定到刚刚创建的容器
    • 向下添加下一级
    • 重复,直到你完成
类似这样的情况(未经测试-我这里没有服务器可以检查):

我看到一个问题:如果您创建一个新的OU,您可能无法立即绑定到它——有时需要一些时间才能传播到目录中

这有用吗


Marc

在过去遇到过类似的情况,我创建了一个函数,该函数将返回目录对象,无论是新创建的还是现有的(很抱歉,它是在VBScript中):

使用此方法,我可以构建我的链,而不考虑OU是否存在。首先设置基本路径,然后在此基础上添加:

Set objDomain = GetObject(LDAP_CONNECTION_STRING)
Set parentOU = GetOU(objDomain, "parentOU")
Set childOU = GetOU(parentOU, "childOU")
Set subchildOU = GetOU(childOU, "subchildOU")

代码运行时,OU的set命令要么是新建的,要么是绑定到的(如果它们已经存在)。

嗨,ecoffey,我已经编辑了代码,修复了丢弃DirectoryEntry的问题,谢谢。正如我在第一篇文章中提到的,我不能使用strOUArray[x]在//问题行中,因为数组中的第一项不存在,所以我无法在此父位置创建OU。您还有什么想法吗?谢谢
Function GetOU(objParentOU, strOUName)
    On error resume next
    Set GetOU = Nothing

    objParentOU.Filter = Array("organizationalUnit")
    Dim OU : Set OU = Nothing
    For Each OU in objParentOU
        if lcase(OU.Name) = lcase("ou=" & strOUName) then
            wscript.echo "Connected to existing OU: " & OU.Name
            set GetOU = GetObject(OU.adsPath)
            exit function
        end if
    Next
    Set OU = Nothing

    ' If script made it to here then the OU must not already exist
    Set GetOU = objParentOU.Create("organizationalUnit", "ou=" & strOUName)
    GetOU.SetInfo

    if err then
        wscript.echo err.description
        err.clear
    else
        wscript.echo "Created new OU: " & strOUName
    end if
end function
Set objDomain = GetObject(LDAP_CONNECTION_STRING)
Set parentOU = GetOU(objDomain, "parentOU")
Set childOU = GetOU(parentOU, "childOU")
Set subchildOU = GetOU(childOU, "subchildOU")