VB.net Active Directory User Attributes

1,247 views
Skip to first unread message

cbtg2006

unread,
Feb 2, 2007, 10:32:21 AM2/2/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi,

I'm currently writing an app that will automate the process of
creating new users. I need ot be able to add terminalserverprofile and
home properties to all user accounts created.

I have managed this in vb script wihthout too many problems but I am
finding this almost impossible to find information on how to do this
in vb.net and my code from my vbscript is not portable to vb.net.

We're running a Server 2000 based AD. The following VBScript works
perfectly:

------------------------------------------------------------------------
Set oRoot = GetObject("LDAP://rootDSE")
Set oDomain = GetObject("LDAP://" & oRoot.Get("defaultNamingContext"))
Set oOU=oDomain.Create("organizationalUnit", strOU)

Set oUser = oOU.Create("User", "CN=" & FirstName & " " & Surname)
oUser.put "sAMAccountName", LCase(UserName)
oUser.put "displayName", FirstName & " " & Surname
oUser.put "description", Department
oUser.setinfo
oUser.userPrincipalName = UserName & "@" & DomainName
oUser.put "givenName", FirstName
oUser.put "sn", Surname
oUser.setpassword DefaultPassword
oUser.loginscript = LogonScript
oUser.accountdisabled = False
oUser.setinfo
oUser.profile = ProfileServer & LCase(UserName)
oUser.TerminalServicesProfilePath = ProfileServer & LCase(UserName)
oUser.setinfo
oUser.TerminalServicesHomeDrive = "V:"
oUser.TerminalServicesHomeDirectory = HomeFileServer &
LCase(UserName)
oUser.setinfo
---------------------------------------------------------------------------

In ym VB.net code I am using the following to set user attributes:

---------------------------------------------------------------------------

Dim MyDirectory As New
DirectoryServices.DirectoryEntry("LDAP://" & OULocation,
strADUserAccount, strADPassword)
Dim strNewDSAUser As DirectoryServices.DirectoryEntry =
MyDirectory.Children.Add("CN=" & FirstName & " " & Surname, "user")

strNewDSAUser.Properties("sAMAccountName").Value =
UserName
strNewDSAUser.Properties("userPrincipalName").Add(UserName
& "@" & DomainName)
strNewDSAUser.Properties("givenName").Value = FirstName
strNewDSAUser.Properties("sn").Value = Surname
strNewDSAUser.Properties("displayName").Value = FirstName
& " " & Surname
strNewDSAUser.Properties("description").Value = Department
strNewDSAUser.CommitChanges()
strNewDSAUser.Invoke("setPassword", DefaultPassword)
strNewDSAUser.CommitChanges()
strNewDSAUser.Properties("Profile").Value = ProfileServer
& LCase(UserName)

strNewDSAUser.Properties("TerminalServicesProfilePath").Value =
ProfileServer & LCase(UserName)
strNewDSAUser.CommitChanges()
-------------------------------------------------------------------------

If I try to set the property TerminalServicesProfilePath in vb.net I
get the following error:

The specified directory service attribute or value does not exist.
(Exception from HRESULT: 0x8007200A)


If anyone has any pointers I would be very grateful!

Many thanks,

Bradley, Peter

unread,
Feb 2, 2007, 11:27:32 AM2/2/07
to DotNetDe...@googlegroups.com
You need to look at System.DirectoryServices. Specifically, the class
and method that you're interested in is DirectoryEntry.Invoke(). So
your code will look something like this (I've used some attributes from
our own AD: you'll obviously want to pick your own):

using System.DirectoryServices;

...

userEntry.Invoke("Put", "samAccountName", user.SamAccountName);
userEntry.Invoke("Put", "userPrincipalName", user.UserPrincipalName);
userEntry.Invoke("Put", "sn", user.Sn);
userEntry.Invoke("Put", "givenName", user.GivenName);
userEntry.Invoke("Put", "mail", user.Mail);
userEntry.Invoke("Put", "displayName", user.DisplayName);
userEntry.Invoke("Put", "profilepath", user.ProfilePath);
userEntry.Invoke("Put", "homedirectory", user.HomeMDB);
userEntry.Invoke("Put", "homeDrive", user.HomeDrive);
userEntry.Invoke("Put", "title", user.Title);
userEntry.CommitChanges();

...

(The user variable above is just an instance of a utility class we've
defined called UserDetails, so it's just an object holding the details
we're interested in. All the properties return a string.)

HTH


Peter

cbtg2006

unread,
Feb 5, 2007, 4:09:09 AM2/5/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi Peter,

Thanks for getting back to me so soon.

I have tried this and I am still recieving the following error:

The specified directory service attribute or value does not exist.
(Exception from HRESULT: 0x8007200A)

Can I ask how you are connecting to the Directory; as previsouly
stated I am using:

Dim MyDirectory As New DirectoryServices.DirectoryEntry("LDAP://" &

OULocation,strADUserAccount, strADPassword)


Dim strNewDSAUser As DirectoryServices.DirectoryEntry =
MyDirectory.Children.Add("CN=" & FirstName & " " & Surname, "user")

Maybe this could eb the problem?

All other values are wrting correctly, just thr TS attributes. I am
ssuming this must be possible as I can do this in vb script.

Many thanks,

Chris

Bradley, Peter

unread,
Feb 5, 2007, 8:17:09 AM2/5/07
to DotNetDe...@googlegroups.com
This is how we connect:

// This is actually obtained from a config file
private const string HomePath =
"LDAP://<server>.internal.<us>.ac.uk:389";

...

DirectoryEntry root = new DirectoryEntry(HomePath);

object userObj = root.Invoke("Create", "user",
userDirectory.ToString());
if (userObj is DirectoryEntry)
{
userEntry = (DirectoryEntry)userObj;
}
else
{
userEntry = new DirectoryEntry(userObj);

cbtg2006

unread,
Feb 6, 2007, 1:57:37 PM2/6/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Still no joy :(

All user attributes are written and then it bombs out on the
terminalservices properties.

Thanks for your help thus far Peter!

-Chris

Paul

unread,
Feb 7, 2007, 8:23:36 AM2/7/07
to DotNetDe...@googlegroups.com
Chris,
   I just scanned this thread... I have had several issues with AD as well. I wrote an app similar to this many moons ago. Try returning all users and scrolling thru the attributes returned (i.e. Debug.WriteLine) to find exactly how AD describes the attribute you are looking for.
   As you have stated, there isn't much info out there to research. Lots of hair pulling. It usually comes down to how AD has been set up and how it is being maintained.
 
HTH
 
Paul

 
>   Dim MyDirectory As New DirectoryServices.DirectoryEntry ("LDAP://" &

> OULocation,strADUserAccount, strADPassword)
>   Dim strNewDSAUser As DirectoryServices.DirectoryEntry =
> MyDirectory.Children.Add("CN=" & FirstName & " " & Surname, "user")
>
> Maybe this could eb the problem?
>
> All other values are wrting correctly, just thr TS attributes. I am
> ssuming this must be possible as I can do this in vb script.
>
> Many thanks,
>
> Chris
>
> On Feb 2, 4:27 pm, "Bradley, Peter" <pbrad...@uwic.ac.uk> wrote:
> > You need to look at System.DirectoryServices.  Specifically, the class
> > and method that you're interested in is DirectoryEntry.Invoke().  So
> > your code will look something like this (I've used some attributes
> from
> > our own AD: you'll obviously want to pick your own):
>
> > using System.DirectoryServices;
>
> > ...
>
> > userEntry.Invoke("Put", "samAccountName", user.SamAccountName);
> > userEntry.Invoke("Put", "userPrincipalName", user.UserPrincipalName);
> > userEntry.Invoke("Put", "sn", user.Sn);
> > userEntry.Invoke("Put", "givenName", user.GivenName);
> > userEntry.Invoke("Put", "mail", user.Mail);
> > userEntry.Invoke("Put", "displayName", user.DisplayName);
> > userEntry.Invoke("Put", "profilepath", user.ProfilePath);
> > userEntry.Invoke ("Put", "homedirectory", user.HomeMDB);
> > strNewDSAUser.Properties ("TerminalServicesProfilePath").Value =

> > ProfileServer & LCase(UserName)
> >             strNewDSAUser.CommitChanges()
>
> ------------------------------------------------------------------------
> > -
>
> > If I try to set the property TerminalServicesProfilePath in vb.net I
> > get the following error:
>
> > The specified directory service attribute or value does not exist.
> > (Exception from HRESULT: 0x8007200A)
>
> > If anyone has any pointers I would be very grateful!
>
> > Many thanks,



that has been needlessly slaughtered.

cbtg2006

unread,
Feb 12, 2007, 12:33:34 PM2/12/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Well unbelivebaly I got this working, despite finding several sites
that expressed it was not possible. It appears that the process will
only work when the TerminalServer attributes are set prior to
committing the changes to the diectory. If the user object is created
and committed this attribute cannot be set....

I have copied the code below for reference.... Now I'm stuck on
mailbox creation using CDOEXM and IMailboxStore - I'll setup a new
thread! ... when it rains it pours!

Dim MyDirectory As New DirectoryServices.DirectoryEntry("LDAP://" &
OULocation, strADUserAccount, strADPassword)
Dim strNewDSAUser As DirectoryServices.DirectoryEntry =
MyDirectory.Children.Add("CN=" & FirstName & " " & Surname, "user")


strNewDSAUser.Invoke("Put", "sAMAccountName", UserName)
strNewDSAUser.Invoke("Put", "displayName", FirstName & " " & Surname)
strNewDSAUser.Invoke("Put", "givenName", FirstName)
strNewDSAUser.Invoke("Put", "sn", Surname)
strNewDSAUser.Invoke("Put", "mail", FirstName & "." & Surname & "@" &
DomainName)
strNewDSAUser.Invoke("Put", "givenName", FirstName)

If CreateProfileFolder = True Then
strNewDSAUser.InvokeSet("TerminalServicesProfilePath",
ProfileServer & LCase(UserName))
strNewDSAUser.InvokeSet("Profile", ProfileServer &
LCase(UserName))
End If
If CreateHomeFolder = True Then
strNewDSAUser.Invoke("Put", "homeDrive", "V:")
strNewDSAUser.Invoke("Put", "homeDirectory",
HomeFileServer & LCase(UserName))
strNewDSAUser.InvokeSet("TerminalServicesHomeDrive",
"V:")

strNewDSAUser.InvokeSet("TerminalServicesHomeDirectory",
HomeFileServer & LCase(UserName))
End If

strNewDSAUser.CommitChanges()
'Password cannot be set until user object exists in AD.
strNewDSAUser.Invoke("SetPassword", DefaultPassword)
strNewDSAUser.CommitChanges()

-Chris

> > Subject: [DotNetDevelopment] Re: VB.netActiveDirectoryUser Attributes


>
> > Hi Peter,
>
> > Thanks for getting back to me so soon.
>
> > I have tried this and I am still recieving the following error:
>

> > The specifieddirectoryservice attribute or value does not exist.
> > (Exception from HRESULT: 0x8007200A)
>
> > Can I ask how you are connecting to theDirectory; as previsouly

> > > Subject: [DotNetDevelopment] VB.netActiveDirectoryUser Attributes

> > > The specifieddirectoryservice attribute or value does not exist.

Reply all
Reply to author
Forward
0 new messages