Skip to content

Creating a user in Active Directory

Chris Cata

Asked Jul 10, 2008, 6:19 AM EDT4 answers
Share
LinkedInX

Hi, I'm new here and I just have a couple questions about this code below.  It's not mine so I am not taking any credit for it, however I can't get it to work.  I just had a question as to what may be wrong with the code below and what can be done to fix it.  I had it working literally 2 days ago, but I made some changes and started passing some other data to it(username, email, etc.).  Before I just entered it in manually.

Anyways, some of the errors look like so:

A Device attached to the system is not function(HRESULT: 0x8007001F).  Now I think we all know that doesn't make sense and isn' actually related to the error at hand.  I've done some searching around and others have come up with the same conclusion.  However, paying attention to the line number it errors out on it's NewUser.CommitChanges().    I read somewhere else that it's because I didn't specificy a DN for the user.  How do I do this here?  I am thinking it may have been specified before but when I made some changes I removed the code. 

 

-------------

Try

Dim AD As DirectoryEntry = _

New DirectoryEntry("LDAP://mydomain.com/OU=Users,DC=md,DC=ad,DC=mydomain,DC=com", domain & "\" & loginName, pwd)

Dim NewUser As DirectoryEntry = AD.Children.Add("CN=" & dUsername, "user")

NewUser.Properties("SAMAccountName").Value = dUsername

NewUser.CommitChanges()

NewUser.Invoke("SetPassword", New Object() {dPassword})  <--- Problem appears to be here or...

NewUser.Invoke("Put", New Object() {"Description", "FTP"})

NewUser.CommitChanges()  <---- Or here.

PowerShell
Dim grp As DirectoryEntry
grp = AD.Children.Find("GG FTP Public", "group")
If grp.Name <> "" Then
grp.Invoke("Add", New Object() {NewUser.Path.ToString()})
End If
MessageBox.Show("Account Created Successfully")
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try

4 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Did you figure out what the problem is yet? I'm having the same issue...
    0
  • 0

    Yes I did.  I went back to basics and started to output everything that was getting entered in as a variable.  The database I was taking the values from wasn't configured properly.  In short, when entering data into AD you need to make sure you cover all the variables correctly.  In my case, the username I was trying to enter, was too long.

     

    "Test User" actually was being entered as "Test User                       ".  With all the spaces in there I couldn't figure out what was going on.  Tried different methods of changing the output and finally went back to the database.  It wasn't getting entered in as text right, no matter what the entry was the text coming out was the full 30 characters alloted long.  Fixed that, code worked right away.

    0
  • 0
    Humm, thats not my problem, but thanks for the update.
    0
  • 0

    Well, as a tip then you can try to CommitChanges() at difference stages of the code tos ee what is and isn't work.

    0