views:

118

answers:

2

It seems that the changes are not saving within ActiveDirectory despite me using the CommitChanges function. Am I using the correct approach to solve this issue?

static void Main(string[] args)
    {
        //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();
    }
+1  A: 

Well, you're grabbing the underlying object, modifying it, and then grabbing it one more time and calling CommitChanges() on it.... I think this won't work this way.

Try this:

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 that change anything? Does it work now??

marc_s
I can't believe I made this mistake. Thank you for your help.
Ishmael
@Ishmael: I guess we've all been there - our head so deep in code, we don't see the most obvious mistakes anymore - glad I was able to help!
marc_s
A: 

Hello, this Code in my Program

 IADsTSUserEx entryX = (IADsTSUserEx)resultEntry.NativeObject; 

throw this Exception

The COM object of type System.__ComObject "can not be cast to the interface type" TSUSEREXLib.IADsTSUserEx. This operation could not be performed because the QueryInterface call on the COM component for the interface with IID '{C4930E79-2989-4462-8A60-2FCF2F2955EF} "due to error could not be done: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

chris