Hi!
You're in luck, as I wrote this function just over a week ago
Add a reference to the System.DirectoryServices (DLL)
Imports System.DirectoryServices
Private Enum ADSI_Flags
ADS_UF_SCRIPTADS_UF_SCRIPT = 1
ADS_UF_ACCOUNTDISABLEADS_UF_ACCOUNTDISABLE = 2
ADS_UF_HOMEDIR_REQUIREDADS_UF_HOMEDIR_REQUIRED = 8
ADS_UF_LOCKOUTADS_UF_LOCKOUT = 16
ADS_UF_PASSWD_NOTREQDADS_UF_PASSWD_NOTREQD = 32
ADS_UF_PASSWD_CANT_CHANGEADS_UF_PASSWD_CANT_CHANGE = 64
ADS_UF_ENCRYPTED_TEXT_PASSWORD_ALLOWEDADS_UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED
= 128
ADS_UF_TEMP_DUPLICATE_ACCOUNTADS_UF_TEMP_DUPLICATE_ACCOUNT = 256
ADS_UF_NORMAL_ACCOUNTADS_UF_NORMAL_ACCOUNT = 512
ADS_UF_INTERDOMAIN_TRUST_ACCOUNTADS_UF_INTERDOMAIN_TRUST_ACCOUNT =
2048
ADS_UF_WORKSTATION_TRUST_ACCOUNTADS_UF_WORKSTATION_TRUST_ACCOUNT =
4096
ADS_UF_SERVER_TRUST_ACCOUNTADS_UF_SERVER_TRUST_ACCOUNT = 8192
ADS_UF_DONT_EXPIRE_PASSWDADS_UF_DONT_EXPIRE_PASSWD = 65536
ADS_UF_MNS_LOGON_ACCOUNTADS_UF_MNS_LOGON_ACCOUNT = 131072
ADS_UF_SMARTCARD_REQUIREDADS_UF_SMARTCARD_REQUIRED = 262144
ADS_UF_TRUSTED_FOR_DELEGATIONADS_UF_TRUSTED_FOR_DELEGATION = 524288
ADS_UF_NOT_DELEGATEDADS_UF_NOT_DELEGATED = 1048576
ADS_UF_USE_DES_KEY_ONLYADS_UF_USE_DES_KEY_ONLY = 2097152
ADS_UF_DONT_REQUIRE_PREAUTHADS_UF_DONT_REQUIRE_PREAUTH = 4194304
ADS_UF_PASSWORD_EXPIREDADS_UF_PASSWORD_EXPIRED = 8388608
ADS_UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATIONADS_UF_TRUSTED_TO_AUTHENTICATE_
FOR_DELEGATION = 16777216
End Enum
Private Function AddUser(ByVal sLogin As String, ByVal sPassword As
String) As Boolean ', ByVal sGroup As String) As Boolean
Dim dirEntry As DirectoryEntry
dirEntry = New DirectoryEntry("WinNT://" + Environment.MachineName +
",computer")
Dim entries As DirectoryEntries = dirEntry.Children
' Set login name and full name.
Dim newUser As DirectoryEntry = entries.Add(sLogin, "User")
Try
'newUser.Properties("FullName").Add(fullName)
'newUser.Properties("HomeDirectory").Add("C:\TestDirectory")
newUser.Properties("Description").Add("My User's Description
Here")
' User must change password at next logon (1 - true, 0 - false)
newUser.Properties("PasswordExpired").Add(0)
' Password never expires.
'newUser.Properties("PasswordAge").Add(0)
' Set flags - User Cannot change password | Password never
expires.
newUser.Properties("Userflags").Add(ADSI_Flags.ADS_UF_PASSWD_CANT_CHANGEADS_
UF_PASSWD_CANT_CHANGE Or
ADSI_Flags.ADS_UF_DONT_EXPIRE_PASSWDADS_UF_DONT_EXPIRE_PASSWD)
'newUser.Properties("Userflags").Add(&H40 Or &H10000)
'newUser.Properties("Userflags").Add(64 Or 65536)
' Set the password.
Dim result As Object = newUser.Invoke("SetPassword", sPassword)
newUser.CommitChanges()
' Add user to the group "Members"
Dim grp As DirectoryEntry =
dirEntry.Children.Find("Administrators", "group")
If (Not grp Is Nothing) Then
grp.Invoke("Add", New Object() {newUser.Path.ToString()})
End If
' Dim usrGroup As DirectoryEntry =
dirEntry.Children.Find("Users", "group")
' If (Not usrGroup Is Nothing) Then
' usrGroup.Invoke("Add", New Object()
{newUser.Path.ToString()})
' End If
Return True
Catch ex As Exception
Return False
End Try
End Function
Example:
---------
AddUser("My USername Here", "My Password Here")
That will add a user if not already there, otherwise, it will fail
If you remove the commit changes & a few lines above & then use the 'If'
statement to check to see if the user is in the 'Administrators' group, if
not the line between the 'If' statement will add the user to the
'Administrators' group.
I hope this helps,
Crouchie1998
BA (HONS) MCP MCSE