I am trying to create a Principal like this:
PrincipalContext pc = new PrincipalContext(ContextType.Machine);
GroupPrincipal group = new GroupPrincipal(pc);
group.Name = "Some Group Name";
group.Description = "Some Group Name Description";
group.Save();
However, when the code is executed, I get the following exception message:
System.DirectoryServices.AccountManagement: Property is not valid for this store type.
If I do not set the Description
property, the above code works perfectly fine, just doesn’t have a description for a group.
Am I doing something wrong?
Thanks in advance.
EDIT: I believe I have found a work-around for this (for anyone who maybe interested). A group can be created in the same way as above:
PrincipalContext pc = new PrincipalContext(ContextType.Machine);
GroupPrincipal group = new GroupPrincipal(pc);
group.Save();
Now you create a DirectoryEntry
and link it to the newly created Group like this:
string path = "WinNT://" + machineName + "/" + group.SamAccountName;
DirectoryEntry dEntry = new DirectoryEntry(path);
This allows access to the Properties of that group, but the one I was interested in is Description, so:
dEntry.Properties["description"].Add("Some Decription");
dEntry.CommitChanges();
And that should do it.
Thank you for your help Abel, It has pushed me to find the workaround :) Hope someone finds this useful.