1

Question : How do I delete a group from active directory?


What I have tried:

1. PrinipalContext

I am trying to delete an active directory group. I have this right now:

using (var ctx = new PrincipalContext(ContextType.Domain, myDomain, ldapUser, ldapPassword))
{
    var group1 = new GroupPrincipal(ctx, groupName);
    group1.Delete();
}

But I get an error: "Unpersisted Principal objects can not be deleted."

That lead me here, but I don't know what the invoke magic is all about and it scares me a little bit.

2. DirectoryEntry

http://www.codeproject.com/Articles/18102/Howto-Almost-Everything-In-Active-Directory-via-C#33

But I just kept getting "The server is not operational" errors.

I just need to delete the AD group, is it even possible?

Community
  • 1
  • 1
wilsjd
  • 2,178
  • 2
  • 23
  • 37

1 Answers1

0

Turns out that the DirectoryEntry works fine, but my ldap urls were wrong. Here is the code that I ended up using:

using (var ou = new DirectoryEntry(ouPath, ldapUser, ldapPassword))
{
     using (var group = new DirectoryEntry(groupPath, ldapUser, ldapPassword))
     {
          ou.Children.Remove(group);
          group.CommitChanges();
     }
}

WRONG OLD VALUES

ouPath LDAP://myDomain.local/OU=myTier1,DC=myDomain,DC=local

groupPath LDAP://groupname/OU=myTier3,OU=myTier2,OU=myTier1,DC=myDomain,DC=local

CORRECT NEW VALUES

ouPath LDAP://myDomain.local/OU=myTier2,OU=myTier1,DC=myDomain,DC=local

groupPath LDAP://myDomain.local/CN=groupName,OU=myTier3,OU=myTier2,OU=myTier1,DC=myDomain,DC=local

wilsjd
  • 2,178
  • 2
  • 23
  • 37