Hello,
For those who are interested in creating Account in Active Directory here is
some code I used.
ublic Function create_user(ByVal firstname As String, ByVal lastname As
String, _
ByVal Address As String, ByVal pwd As String)
Try
Dim MyRoot As DirectoryServices.DirectoryEntry
Dim MyUser As DirectoryServices.DirectoryEntry
Dim MySearcher As DirectorySearcher
Dim MyResult As SearchResult
Dim Loginname As String
Dim x As Integer
x = 1
Loginname = Replace(lastname, " ", "")
MyRoot = New
DirectoryServices.DirectoryEntry("LDAP://ASDVM01.milkyway.com",
"milkyway\administrator", "knowledge")
'create search option(looks if the user already exist)
MySearcher = New DirectorySearcher(MyRoot)
MySearcher.Filter = "(userPrincipalName=" & Loginname & ")"
MyResult = MySearcher.FindOne()
Do While Not MyResult Is Nothing
If x <= Len(firstname) Then
Loginname = Replace(lastname, " ", "") & "-" &
Left(firstname, x)
Else
Loginname = Replace(lastname, " ", "") & "-" & firstname
& CStr(x - Len(firstname))
MsgBox(Loginname)
End If
MySearcher.Filter = "(userPrincipalName=" & Loginname & ")"
MyResult = MySearcher.FindOne()
x = x + 1
Loop
'add user properties to the new user
MyUser = MyRoot.Children.Add("CN=" & Loginname, "user")
MyUser.Properties("GivenName").Add(firstname)
MyUser.Properties("sn").Add(lastname)
MyUser.Properties("DisplayName").Add(firstname & " " & lastname)
MyUser.Properties("name").Add(Loginname)
MyUser.Properties("sAMAccountName").Add(Loginname)
MyUser.Properties("userPrincipalName").Add(Loginna me &
"@milkyway.com")
MyUser.Properties("streetaddress").Add(Address)
MyUser.Properties("mail").Add(firstname & "." &
Replace(lastname, " ", "") & "@milkyway.com")
MyUser.CommitChanges()
MyUser.NativeObject.AccountDisabled = False
MyUser.CommitChanges()
MyUser.Invoke("SetPassword", pwd)
MyUser.CommitChanges()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Function
this code checks if the user already exist if so it will create an new
account with lastname + fist char of the firstname.
Hopefully this code will help someone
Have fun!!!
Martijn