Does commitChanges () call do nothing in Active Directory?

It seems that the changes are not saved in ActiveDirectory even though I use the CommitChanges function. Am I using the right approach to solve this problem?

//Test OU Group: OU=First Group,OU=Domain Users,DC=DOMAIN,DC=com
String userName, password;

Console.Write("Username: ");
userName = Console.ReadLine();
Console.Write("Password: ");
password = Console.ReadLine();

//ENTER AD user account validation code here

String strLDAPpath = "LDAP://OU=First Group,OU=Domain Uers,DC=DOMAIN,DC=com";
DirectoryEntry entry = new DirectoryEntry(strLDAPpath,userName,password,AuthenticationTypes.Secure);
//DirectoryEntry entry = new DirectoryEntry(strLDAPpath);
DirectorySearcher mySearcher = new DirectorySearcher(entry);
mySearcher.Filter = "(ObjectCategory=user)";
foreach (SearchResult result in mySearcher.FindAll())
{
    IADsTSUserEx entryX = (IADsTSUserEx)result.GetDirectoryEntry().NativeObject;
    int isTrue = 1;
    entryX.ConnectClientDrivesAtLogon = isTrue;
    entryX.ConnectClientPrintersAtLogon = isTrue;
    entryX.DefaultToMainPrinter = isTrue;
    result.GetDirectoryEntry().CommitChanges();        
}
Console.WriteLine("Changes have been made. Press Enter to continue...");
Console.ReadLine();
////entry = new DirectoryEntry(strLDAPpath, userName, password, AuthenticationTypes.Secure);
//mySearcher = new DirectorySearcher(entry);
//mySearcher.Filter = "(ObjectCategory=user)";
foreach(SearchResult result in mySearcher.FindAll())
{
    IADsTSUserEx entryX = (IADsTSUserEx)result.GetDirectoryEntry().NativeObject;
    Console.WriteLine(result.GetDirectoryEntry().Properties["name"].Value + "\t" + "Drives " + entryX.ConnectClientDrivesAtLogon + "\t" + "Printers " + entryX.ConnectClientPrintersAtLogon + "\t" + "Default " + entryX.DefaultToMainPrinter);
}
entry.Close();
Console.ReadLine();
+3
source share
1 answer

Ok, you grab the base object, modify it, and then grab it again and call CommitChanges () on it ... I think this will not work.

Try the following:

mySearcher.Filter = "(ObjectCategory=user)";

foreach (SearchResult result in mySearcher.FindAll())
{
     DirectoryEntry resultEntry = result.GetDirectoryEntry();
     IADsTSUserEx entryX = (IADsTSUserEx)resultEntry.NativeObject;

     int isTrue = 1;
     entryX.ConnectClientDrivesAtLogon = isTrue;
     entryX.ConnectClientPrintersAtLogon = isTrue;
     entryX.DefaultToMainPrinter = isTrue;

     resultEntry.CommitChanges();        
 }

Does it change anything? Does it work now?

+2
source

Source: https://habr.com/ru/post/1730087/


All Articles