04
Nov
08

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;
entry.CommitChanges();
entry.Close();
}
}
}

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

Advertisements

0 Responses to “when 3.5 isnt good enough”



  1. Leave a Comment

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


November 2008
M T W T F S S
« Oct   Apr »
 12
3456789
10111213141516
17181920212223
24252627282930

Greyeye Tweets


%d bloggers like this: