Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
With the exception of the creating a homedirectory this code works.
Can anyone give me the correct format for the homedirectory value?
this one give me the correct userdata on the profile page in AD
Properties("homedirectory").Add("\\TestServer2\Home_Folders$\" & sUserName)
but the users folder is not created on the members server TestServer2.

What I have tried:

VB.NET
Public Sub CreateAdAccount(ByVal sUserName As String, ByVal sPassword As String, ByVal sFirstName As String, ByVal sLastName As String, ByVal sGroupName As String, ByVal Ou As String)
Try
' SaveTextToFile("1", AppLogPath)
Dim StrRoot As String = "LDAP://" & Ou & ""
Dim root As New DirectoryEntry(StrRoot)
Dim newUser As DirectoryEntry = root.Children.Add("CN=" & sUserName, "user")
SetProperty(newUser, "description", "Some Test")
SetProperty(newUser, "givenname", sFirstName)
SetProperty(newUser, "sn", sLastName)
SetProperty(newUser, "SAMAccountName", sUserName)
SetProperty(newUser, "DisplayName", Info.DisplayName)
SetProperty(newUser, "userPrincipalName", sUserName & "@TecGroup.org")
SetProperty(newUser, "mail", sUserName & "@TecGroup.org")
SetProperty(newUser, "homedrive", "H:")

'format wrong ?
'SetProperty(newUser, "homedirectory", "@\\TestServer2\Home_Folders$\%UserName%")
'newUser.Properties("homedirectory").Add("\\TestServer2\Home_Folders$\" & sUserName)

newUser.CommitChanges()

newUser.Close()
root.Close()
'write to log
SaveTextToFile(sUserName & " added to AD, password " & sPassword, AppLogPath)

Catch ExtendedErrorMessage As Exception
SaveTextToFile("Add User " & sUserName & " failed, Error " & ExtendedErrorMessage.Message, AppLogPath)
End Try
end sub
Posted
Updated 18-Feb-19 15:24pm
v2
Comments
CHill60 20-Feb-19 9:18am    
Do you have admin permissions on the domain? If not then you can't do anything with other users' folders
NoMike2010 20-Feb-19 9:35am    
I can make the users folder and add the appropriate rights with this routine.

Dim Path As String = dInfo.homedirectory & sUserName
If Not Directory.Exists(Path) Then
Directory.CreateDirectory(Path)
If Directory.Exists(Path) Then
Dim FolderInfo As IO.DirectoryInfo = New IO.DirectoryInfo(Path)
Dim FolderAcl As New DirectorySecurity
FolderAcl.AddAccessRule(New FileSystemAccessRule(UserAccount, FileSystemRights.Modify, InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow))
'FolderAcl.SetAccessRuleProtection(True, False) 'uncomment to remove existing permissions
FolderInfo.SetAccessControl(FolderAcl)
End If
End if

I was under the impression that when adding the homedirectory property, the folder
should have been created automatically, but that may not be correct.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900