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,
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
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
// 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);
All user attributes are written and then it bombs out on the
terminalservices properties.
Thanks for your help thus far Peter!
-Chris
> 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.
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.