Posts Tagged ‘adsi


when 3.5 isnt good enough

I like new functions in framework but every new version has SOMETHING thats missing where I need most.

.net 3.5 came out with nice/better wrapper for the AD modification, yet it didnt give me the function to modify all the properties especially the tsprofilepath.

well I’ve found the alternative but you need to import DLL(Interop.TSUSEREXLIB.DLL) from here

then use the code below

using TSUSEREXLib;
using System.DirectoryServices;
using System;
using System.Collections.Generic;
using System.Text;

namespace Set_Terminal_Service_Properties
class Program
static void Main(string[] args)
string acctName = “chrisca”;
string tsHomeDrive = “H:”;
string tsHomeDirectory = “\\\\servername\\tshomedirectory\\”;
string tsProfilePath = “\\\\servername\\tsprofilepath\\”;
int enableLogon = 1; // enable terminal service logins

DirectoryEntry entry = new DirectoryEntry
(“LDAP://CN=Chris Calderon,CN=Users,DC=corp,DC=contoso,DC=com”);
ADsTSUserEx oUser = (ADsTSUserEx)entry.NativeObject;
oUser.AllowLogon = enableLogon;
oUser.TerminalServicesHomeDirectory = tsHomeDirectory + acctName;
oUser.TerminalServicesHomeDrive = tsHomeDrive;
oUser.TerminalServicesProfilePath = tsProfilePath + acctName;

Alternatively, my code looks like below (need reference to System.DirectoryServices.AccountManagement in .net 3.5)

var context = new PrincipalContext(ContextType.Domain);
var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, UserNameBox.Text);
Debug.WriteLine(“UserNameBox” + UserNameBox.Text);
ResultBox.Text = ResultBox.Text + Environment.NewLine + “ScriptPath ” + user.ScriptPath;
Debug.WriteLine(“user.GetGroups().Count()” + user.GetGroups().Count());
Debug.WriteLine(“user” + user.DistinguishedName);
foreach (GroupPrincipal group in user.GetGroups())
ResultBox.Text = ResultBox.Text + Environment.NewLine + group.SamAccountName;
DirectoryEntry de = new DirectoryEntry(“LDAP://”+user.DistinguishedName);

ADsTSUserEx tsu = (ADsTSUserEx)de.NativeObject;
Debug.WriteLine(“tspath” + tsu.TerminalServicesHomeDirectory );

Well using Interop will be messy but I think its better than writing process execute code to start vbs. (Alternative method is to use invokeSet, but I could not find better example so I’ve passed using it.)

Above original Code is refered from here