Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change AD user Terminal Server properties with C#

I am using System.DirectoryServices.DirectoryEntry to create AD user and everything work fine except for some Remote Desktop specifics properties.

Exemple :

newUser.Properties["msTSConnectClientDrives"].Value = false;
newUser.Properties["msTSConnectPrinterDrives"].Value = false;
newUser.Properties["msTSDefaultToMainPrinter"].Value = false;

This doesn't throw any exception, so I guess the properties are found in the object but they don't have any effect. When I go into the property window of that user, under "Environment" tab, these 3 checkbox are still checked on.

Am I missing something particular for these properties ?

Thank for your help.

EDIT :

Sorry I have been really busy, here is a code sample :

    private string CreateNewADAccount(string accountName, string accountPassword)
    {
        try
        {
            PrincipalContext context = new PrincipalContext(ContextType.Domain, "SV-LITE", @"LITE\xxxxxxxx", "yyyyyyyy");

            UserPrincipal newUser = new UserPrincipal(context);
            newUser.SamAccountName = accountName;
            newUser.UserPrincipalName = accountName;
            newUser.Name = "LiteUser2015 - " + accountName;
            newUser.DisplayName = "LiteUser2015 - " + accountName;
            newUser.SetPassword(accountPassword);
            newUser.PasswordNeverExpires = true;
            newUser.UserCannotChangePassword = true;

            newUser.Save();

            // Set advanced properties
            if (newUser.GetUnderlyingObjectType() == typeof(DirectoryEntry))
            {
                DirectoryEntry entry = (DirectoryEntry)newUser.GetUnderlyingObject();

                entry.Properties["msTSConnectClientDrives"].Value = false;
                entry.Properties["msTSConnectPrinterDrives"].Value = false;
                entry.Properties["msTSDefaultToMainPrinter"].Value = false;
                entry.Properties["msTSInitialProgram"].Value = "test";

                entry.CommitChanges();
            }

            return newUser.Guid.ToString();

        }
        catch (Exception e)
        {
            MessageBox.Show("Failed to create PrincipalContext. Exception: " + e);
        }

        return null;
    }
like image 544
Karnalta Avatar asked Apr 21 '15 13:04

Karnalta


1 Answers

After making the changes, you have to call CommitChanges - newUser.CommitChanges();

See https://msdn.microsoft.com/en-us/library/system.directoryservices.directoryentry.commitchanges%28v=vs.110%29.aspx

By default, changes to properties are made locally to a cache..

like image 54
Dan Avatar answered Nov 05 '22 05:11

Dan