Active Directory, setting password and adding users to group help

Joined
Mar 28, 2005
Messages
17
Programming Experience
Beginner
Hi everyone,

I am using the following code to add users to active directory to the Users container.

VB.NET:
'nd: add new users to the Users container
Dim entry As New DirectoryEntry("[url="ldap://cn=users,dc=xxx,dc=com/"]LDAP://cn=users,dc=xxx,dc=com[/url]")
Dim fName As String = "userFirstName"
Dim lName As String = "userLastName"
Dim myNewUser As DirectoryEntry = entry.Children.Add("cn=" & fName & " " & lName, "user")
'set minimum attributes
With myNewUser.Properties
.Item("name").Value = fName
.Item("sn").Value = lName
.Item("sAMAccountName").Value = fName & "." & lName
.Item("userPrincipalName").Value = fName & "." & lName & "@xxx.com"
.Item("givenName").Value = fName
.Item("distinguishedName").Value = fName & " " & lName
.Item("l").Value = "user location"
End With
myNewUser.CommitChanges()

The user is successfully added to active directory. I also enable the user's account as follows:
VB.NET:
'now enable acccount
Dim userFlag As Integer
Const ADS_UF_DONT_EXPIRE_PASSWD = &H10000
Const ADS_UF_ACCOUNTDISABLE = &H2
userFlag = myNewUser.Properties("userAccountControl").Value
myNewUser.Properties("userAccountControl").Value = userFlag And ADS_UF_DONT_EXPIRE_PASSWD And Not ADS_UF_ACCOUNTDISABLE
myNewUser.CommitChanges()

The user is successfully enabled. However, i want the password to "Password never expires" to be enabled. Here's what i have so far
VB.NET:
'set password
myNewUser.Invoke("SetPassword", New String() {"userPassword"})
txtResult.AppendText("Added")

Also, i am trying to add the users to a specific group. Here's what i have so far:
VB.NET:
'add user to group
Dim myGroup As New DirectoryEntry("LDAP://" & "userGroup")
Dim members As PropertyValueCollection
members.Add(myNewUser.Properties("distinguishedName").Value)
myGroup.CommitChanges()

When i run the program, i receive this error: "Object reference not set to an instance of an object" at line: members.Add(myNewUser.Properties("distinguishedName").Value) when adding the user to group.

I would really appreciate your help. God bless.
 
Top Bottom