Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change a group name (sAMAccountName) in ADS?

I'm Trying to change a name of a group with c# and .NET. It's working well with the following code:

    public void selectADSObject(string LDAP)
    {
        DirectoryEntry Entry = new DirectoryEntry(ADS_PATH);
        Entry.Username = ADS_USER;
        Entry.Password = ADS_PW;
        DirectorySearcher Searcher = new DirectorySearcher(Entry);
        Searcher.SearchScope = System.DirectoryServices.SearchScope.Subtree;
        Searcher.Filter = LDAP;
        AdObj = Searcher.FindOne();
        AdObj.GetDirectoryEntry().Rename("cn=newName");
    }

There is just the "windows-pre 2000" name that doesn't rename and I need it to rename too. On this page I figured out that the sAMAccountName is what I'm after. But when I add the following lines, it also doesn't change the pre-windows 2000 name:

AdObj.GetDirectoryEntry().Properties["sAMAccountName"].Value = "newName";
AdObj.GetDirectoryEntry().CommitChanges();

How can I change the sAMAccountName / pre-windows 2000 name?

like image 753
Jan Hommes Avatar asked Oct 20 '25 06:10

Jan Hommes


1 Answers

Every time you invoke:

AdObj.GetDirectoryEntry()

It actually creates a new object! Every change is lost on the next line. Please use something like:

var dent = AdObj.GetDirectoryEntry()
dent.Properties["sAMAccountName"].Value = "newName";
dent.CommitChanges();
dent.rename("cn=newName");
like image 64
Petesh Avatar answered Oct 22 '25 23:10

Petesh