Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set the ManagedBy property on a GroupPrincipal

I'm creating and updating Groups in Active Directory using the GroupPrincipal class in System.DirectoryServices.AccountManagement. When creating and updating, I also need to be able to set the ManagedBy property that you are able to set in the Managed By tab in the groups properties in the AD management console.

Can it be done programatically?

like image 653
michael_erasmus Avatar asked Jul 20 '10 10:07

michael_erasmus


1 Answers

You cannot do this directly, unfortunately - but you can get access to the underlying DirectoryEntry and do it there:

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN");

UserPrincipal toBeModified = UserPrincipal.FindByIdentity(".....");
UserPrincipal manager = UserPrincipal.FindByIdentity(ctx, "......");

DirectoryEntry de = toBeModified.GetUnderlyingObject() as DirectoryEntry;

if (de != null)
{
    de.Properties["managedBy"].Value = manager.DistinguishedName;
    toBeModified.Save();
}
like image 184
marc_s Avatar answered Sep 22 '22 15:09

marc_s