我需要在需要删除全局组和组织单位的其他要求中创建gui。我有删除正在工作的组的功能,但我也需要能够删除OU。
我正在对组使用以下代码:
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呢?或者我需要做些什么才能做到这一点?
发布于 2020-01-16 00:11:13
这是我过去用来删除OU的代码(,但请谨慎使用)
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.
}
}https://stackoverflow.com/questions/59740008
复制相似问题