flipknob
02-26-2007, 10:54 AM
I have some code that I have used to create a new user in my AD:
------------------------------------------------------------------------
Private Shared Sub AddUser(ByVal strDoamin As String, ByVal strLogin As String, ByVal strPwd As String)
Dim obDirEntry As DirectoryEntry = Nothing
Try
obDirEntry = New DirectoryEntry("WinNT://" + strDomain)
Dim entries As DirectoryEntries = obDirEntry.Children
Dim obUser As DirectoryEntry = entries.Add(strLogin, "User")
obUser.Properties("FullName").Add("Amigo")
Dim obRet As Object = obUser.Invoke("SetPassword", strPwd)
obUser.CommitChanges
Catch ex As Exception
Throw
End Try
End Sub
Shared Sub Main(ByVal args As String())
AddUser("<domin Name>", "<user name>", "<password>")
End Sub
------------------------------------------------------------------------
The problems I'm running into are:
1. How do I make this insert the new user into an alternative AD OU? Do I need to use "LDAP://..." with the path to the OU (OU=Whatever,DC=whatever,DC=com) instead of "WinNt://" for the root?
2. How do I populate the different fields of the user account? For example if the user name is "Steve Doe" how is "Steve" populated into the "First Name" field of the AD account and so on?
3. How would I set this application up so that only certain people in AD security groups (using the current logged in account) can create the new user account? So if Jim (a domain user) tries to add a user it will be rejected but Rick (a member of the domain admins) will be able to.
I know these are tough questions but I'm having no luck with any reference material I'm finding on the web.
Thank you for any input.
Dale
------------------------------------------------------------------------
Private Shared Sub AddUser(ByVal strDoamin As String, ByVal strLogin As String, ByVal strPwd As String)
Dim obDirEntry As DirectoryEntry = Nothing
Try
obDirEntry = New DirectoryEntry("WinNT://" + strDomain)
Dim entries As DirectoryEntries = obDirEntry.Children
Dim obUser As DirectoryEntry = entries.Add(strLogin, "User")
obUser.Properties("FullName").Add("Amigo")
Dim obRet As Object = obUser.Invoke("SetPassword", strPwd)
obUser.CommitChanges
Catch ex As Exception
Throw
End Try
End Sub
Shared Sub Main(ByVal args As String())
AddUser("<domin Name>", "<user name>", "<password>")
End Sub
------------------------------------------------------------------------
The problems I'm running into are:
1. How do I make this insert the new user into an alternative AD OU? Do I need to use "LDAP://..." with the path to the OU (OU=Whatever,DC=whatever,DC=com) instead of "WinNt://" for the root?
2. How do I populate the different fields of the user account? For example if the user name is "Steve Doe" how is "Steve" populated into the "First Name" field of the AD account and so on?
3. How would I set this application up so that only certain people in AD security groups (using the current logged in account) can create the new user account? So if Jim (a domain user) tries to add a user it will be rejected but Rick (a member of the domain admins) will be able to.
I know these are tough questions but I'm having no luck with any reference material I'm finding on the web.
Thank you for any input.
Dale