目录服务只能在叶对象上执行请求的操作。电脑校长

问题描述 投票:0回答:3

我使用下面的代码从 AD 中删除计算机

            using (var ctx = new PrincipalContext(ContextType.Domain, domain, null, null))
            {
                var computer = ComputerPrincipal.FindByIdentity(ctx, computerName);

                if (computer != null)
                    computer.Delete();
            }

我遇到以下错误 “目录服务只能在叶对象上执行请求的操作。” 请帮忙

c#-4.0 active-directory
3个回答
4
投票

这个答案有点晚了,但要补充布莱恩的答案。

这是您访问的方式

DeleteTree()

using (var ctx = new PrincipalContext(ContextType.Domain, domain, null, null))
{
   var computer = ComputerPrincipal.FindByIdentity(ctx, computerName);
   if (computer != null)
   {
      DirectoryEntry en = computer.GetUnderlyingObject() as DirectoryEntry;
      en.DeleteTree();
   }
}

1
投票

这意味着树中计算机下方有一些东西 - 可能类似于发布到目录的共享打印机。 AD 对待删除对象的方式与删除带子对象的对象不同。

因为它看起来不像

ComputerPrincipal
为您提供删除子树的本机选项,所以执行类似
computer.GetUnderlyingObject().DeleteTree()
的操作,您应该会很好。


0
投票
((DirectoryEntry)computer.GetUnderlyingObject()).DeleteTree();

这就是您正在寻找的 Shiv455

© www.soinside.com 2019 - 2024. All rights reserved.