I have a working code for creating a new AD user along with its distinct properties. The only thing I can't get to work is:
The users entry in the AD-Management window (caption) is the same as the CN. But I need the entry to be the same as the DisplayName attribute.
After manually changing the Name of the user entry, I found out, that the attribute 'name' changed as well. (See screenshot) Screenshot-1
So I tried using the attribute 'name' in my code as well. But when I use the attribute during the first CommitChanges() (effectively creating the entitiy) it doesn't set to the Value I want it to be. Instead it stays the same as the login-name (SAMAccountName) of the user.
If I try to set the Value of the attribute during the later CommitChanges() it throws the following exception
"00002016: SvcErr: DSID-031A12D2, problem 5003 (WILL_NOT_PERFORM), data 0" & vbLf
Do you have any Ideas what I can do to change the attribute 'name' of an AD-Object?
(BTW: My code contains some german words. Vorname = Given Name / Nachname = Surname etc. in case you're wondering)
Dim o As DirectoryEntry
Dim c As DirectoryEntries
Dim OUPath = ("LDAP://OU=" & OU & ",OU=Benutzer,DC=int,DC=xxxx,DC=xx")
o = New DirectoryEntry(OUPath, "xxxx", "xxxx", System.DirectoryServices.AuthenticationTypes.Secure)
c = o.Children()
o = c.Add("CN=" & UsernameV, "user")
o.Properties("SAMAccountName").Value = UsernameV
o.Properties("sn").Add(NachnameV)
o.Properties("givenName").Add(VornameV)
o.Properties("displayName").Add("" & NachnameV & ", " & VornameV & "")
o.Properties("name").Add("" & NachnameV & ", " & VornameV & "")
o.Properties("userPrincipalName").Add(UsernameV & "@xxxx")
o.CommitChanges()
o.Invoke("setPassword", New Object() {PWV})
o.CommitChanges()
'newUser.Properties("userAccountControl").Add(512)
'newUser.CommitChanges()
o.Properties("description").Add(UsernameV)
o.Properties("scriptPath").Add("logon.bat")
o.Properties("homeDrive").Add("H:")
o.Properties("homeDirectory").Add("\\datsrv-01\homes\" & UsernameV & "$")
o.Properties("company").Add(ComboBox_so.SelectedItem.ToString)
o.Properties("department").Add(ComboBox_abt.SelectedItem.ToString)
o.Properties("telephoneNumber").Add(TelV)
o.Properties("userAccountControl").Value = 65536
o.CommitChanges()
o.NativeObject.AccountDisabled = False
o.CommitChanges()
MsgBox("Erstellen erfolgreich")