【问题标题】:C# Looping through an array to create Active Directory OUsC# 循环遍历数组以创建 Active Directory OU
【发布时间】:2009-01-03 14:12:11
【问题描述】:

我正在尝试在 Active Directory 中创建嵌套的组织单位,并且我即将完成这项工作。我在下面有一个问题,我需要帮助。我正在检查 OU 是否存在,如果不存在,我需要创建它,strOUArray 包含 OU=Test OU=Test2 和 OU=Test3。我需要代码先创建 OU=Test,然后在下面的 //PROBLEM LINE 上将其用作父 OU,从而允许在 OU=Test 中创建下一个 OU OU=Test2。目前在下面的代码中,所有 OU 都将在根目录中创建,因为我不知道如何在 //PROBLEM LINE 中使用第一个创建的 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();
 }
}

【问题讨论】:

    标签: c# active-directory


    【解决方案1】:

    似乎您的//PROBLEM LINEstrOUArray[x] 排除在字符串连接之外。是发帖的时候打错了吗?

    还有这个sn-p:

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

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

    【讨论】:

    • 嗨,ecoffey,我已经编辑了代码以修复丢弃 DirectoryEntry,谢谢。正如我在第一篇文章中提到的,我不能在 //problem 行上使用 strOUArray[x],因为数组中的第一项不存在,所以我不能在这个父位置创建 OU。你有什么进一步的想法吗?谢谢
    【解决方案2】:

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

    • 首先绑定到顶层(即 LDAP://
    • 创建第一个 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[] { ',' });
    
    // create a bind path which we'll build up
    string ldapPath = dcSubString;
    
    // bind to the top-level container
    DirectoryEntry workEntry = new DirectoryEntry("LDAP://" + ldapPath);
    
    // loop through all sub-OU's to create
    foreach(string currentOU in strOUArray)
    {
      // add the next OU below the current entry
      objOU = workEntry.Children.Add(currentOU, "OrganizationalUnit");
      objOU.CommitChanges(); 
    
      // bind to the newly created OU
      ldapPath = currentOU + "," + ldapPath;
      workEntry = new DirectoryEntry("LDAP://" + ldapPath);
    }
    

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

    这有帮助吗?

    马克

    【讨论】:

      【解决方案3】:

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

      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
      

      使用它,我可以构建我的链,而无需考虑 OU 是否存在。首先设置基本路径,然后在其上添加:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-08
        • 1970-01-01
        • 1970-01-01
        • 2015-11-29
        • 2018-07-17
        • 2021-03-24
        • 2011-01-25
        • 1970-01-01
        相关资源
        最近更新 更多