首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用PrincipalContext删除OU

如何使用PrincipalContext删除OU
EN

Stack Overflow用户
提问于 2020-01-15 02:51:26
回答 1查看 48关注 0票数 1

我需要在需要删除全局组和组织单位的其他要求中创建gui。我有删除正在工作的组的功能,但我也需要能够删除OU。

我正在对组使用以下代码:

代码语言:javascript
复制
 private void btn_verwijderGG_Click(object sender, EventArgs e)
    {
        PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
        GroupPrincipal ou = GroupPrincipal.FindByIdentity(ctx, txt_OUNaam.Text);

        if (ou != null)
        {
            ou.Delete();
            MessageBox.Show("OU" + txt_OUNaam.Text + "is verwijderd");

        }
        else
        {
            MessageBox.Show("OU niet gevonden");
        }
    }

但是如何将te GroupPrincipal更改为ou呢?或者我需要做些什么才能做到这一点?

EN

回答 1

Stack Overflow用户

发布于 2020-01-16 00:11:13

这是我过去用来删除OU的代码(,但请谨慎使用)

  1. 使用根作为SearchBase ...要在某个父OU
  2. 下查找,请使用DirectorySearcher搜索与筛选器
  3. 循环结果匹配的所有OU,然后将其删除。
  4. 确保您具有删除该OU的权限,并且该OU未打开“防止意外保护”。

代码语言:javascript
复制
    public void DeleteOU (string ldap, string filterOU)
    {
        // ldap = "LDAP://DC=fabri,DC=com"
        // filteredOU = "OU=OUName" or "CN=OUName" depending on how its configured.

        DirectoryEntry root = new DirectoryEntry(ldap);
        DirectorySearcher searcher = new DirectorySearcher(root);
        searcher.Filter = $"(&(objectCategory=organizationalUnit)({filterOU}))";

        try
        {
            foreach (SearchResult res in searcher.FindAll())
            {
                DirectoryEntry thisOU = res.GetDirectoryEntry();
                thisOU.DeleteTree();
                thisOU.CommitChanges();
            }
        }
        catch (Exception ex)
        {
            // do something with exception. Most likely, Not Found.
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59740008

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档