我使用下面的代码从 AD 中删除计算机
using (var ctx = new PrincipalContext(ContextType.Domain, domain, null, null))
{
var computer = ComputerPrincipal.FindByIdentity(ctx, computerName);
if (computer != null)
computer.Delete();
}
我遇到以下错误 “目录服务只能在叶对象上执行请求的操作。” 请帮忙
这个答案有点晚了,但要补充布莱恩的答案。
这是您访问的方式
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();
}
}
这意味着树中计算机下方有一些东西 - 可能类似于发布到目录的共享打印机。 AD 对待删除对象的方式与删除带子对象的对象不同。
因为它看起来不像
ComputerPrincipal
为您提供删除子树的本机选项,所以执行类似 computer.GetUnderlyingObject().DeleteTree()
的操作,您应该会很好。
((DirectoryEntry)computer.GetUnderlyingObject()).DeleteTree();
这就是您正在寻找的 Shiv455