Jump to content
EduGeek EdSec 2026 is Go! 27th Oct in Derby! Join us for a day of EdTech security focused talks, networking, and an evening social ×

Recommended Posts

Posted

Ok, my head is frazzled. I have some code in VB.net (2010) that is supposed to create a user. It creates a user fine, but I also have a bulk add where it gets a list of CSV entries and loops through easy calling the function each time, passing the data in from each line of the CSV.

 

This also works fine the first time, but for the 2nd row it fails with A device attached to the system is not functioning. I found it was the following line that caused the error, and commenting it out now added all the users to Active Directory without fail.

 

de.Invoke("Put", New Object() {"sAMAccountName", username})

 

So can anyone tell me why this might be failing - my head is frazzled!

 

Public Shared Function GetDirectoryEntry(Optional ByVal param As String = "") As DirectoryEntry
       ' Connect via LDAP to the server
       Dim dirEntry As New DirectoryEntry("LDAP://LDAPADDRESS/" + param)

       ' Setting these to nothing forces the application to use Windows credentials
       dirEntry.Username = Nothing
       dirEntry.Password = Nothing
       dirEntry.AuthenticationType = AuthenticationTypes.Secure
       ' Return the dirEntry so that other methods can use it
       Return dirEntry
   End Function

Private Sub AddUserToAD(ByVal username As String, ByVal firstname As String, ByVal surname As String, ByVal password As String)

       Dim dirEntry As DirectoryEntry
       'txtLocation.Text is the location in AD such as ou=Test,dc=DOMAIN,dc=internal
       dirEntry = clAD.GetDirectoryEntry(txtLocation.Text)

       Dim entries As DirectoryEntries = dirEntry.Children
       Dim de As DirectoryEntry = entries.Add("cn=" + username, "user")
       de.CommitChanges()

       Dim pass As String = password
       de.Invoke("SetPassword", pass)
       de.Invoke("Put", New Object() {"sAMAccountName", username})
       de.CommitChanges()
     End Sub

Posted

In the command

 

de.Invoke("Put", New Object() {"sAMAccountName", username})

 

You have a variable username but in the sub byvals you only have firstname, surname ( no username )

 

where is it getting username from as I don't see a command that concatenates the forename and surname together to create a username

 

username = firstname & "." & surname

 

or anything of that nature, in the shared function you have username but you are setting it to nothing

 

I have not done much ldap stuff so i might be missing something ??

Posted

I am passing the username variable, here

 

Private Sub AddUserToAD([b]ByVal username As String[/b], ByVal firstname As String, ByVal surname As String, ByVal password As String)

:confused:

 

P.S. The username variable is being passed properly, and is unique - I know this because I have omitted some code that updates display name etc and these work fine.

Posted (edited)

lol doh :doh: - when you call AddUserToAD it is something along the lines of ( obviously after splitting and ascertaining each value from the csv file. )

 

AddUserToAD("John.Smith")

 

I presume you don't need to trim the input from the csv file so that there are no leading or trailing spaces, commas or anything like that ??

 

I presume there are no carriage return or line feed characters seeing as you are saying they are on seperate lines ?

 

Also does it need a pause / sleep time of say half a second or a second between the adding of each user(s) ?

 

In the csv file is it put together like :

 

one, two, three, four

 

OR

 

One,

Two,

Three,

Four

 

etc

Edited by mac_shinobi
  • Thanks 1
Posted
lol doh - when you call AddUserToAD it is something along the lines of ( obviously after splitting and ascertaining each value from the csv file. )

 

AddUserToAD("John.Smith")

 

I presume you don't need to trim the input from the csv file so that there are no leading or trailing spaces, commas or anything like that ??

 

I presume there are no carriage return or line feed characters seeing as you are saying they are on seperate lines ?

 

Also does it need a pause / sleep time of say half a second or a second between the adding of each user(s) ?

 

In the csv file is it put together like :

 

one, two, three, four

 

OR

 

One,

Two,

Three,

Four

 

etc

 

I've just figured out, if I pass "randomstring" as username it works fine, so it must be something in the username field that is passed from CSV - will keep posted.

Posted

WTF! A simple .trim() on the end of username fixed it - why wasn't this falling over for the first line then? Oh, do you think it might have the New Line from the end of the first line attached to the first element of the next line in the CSV?

 

What a doyle I am! :D

Posted
WTF! A simple .trim() on the end of username fixed it - why wasn't this falling over for the first line then? Oh, do you think it might have the New Line from the end of the first line attached to the first element of the next line in the CSV?

 

What a doyle I am! :D

 

This is why I never trust any external data to be correct! It's always a good idea to perform some data integrity routines - yeah it's a PITA to write the extra code sometimes, but it pays off when you avoid headaches like this!

Posted
Yea, watch for trailing spaces, they have messed many a powershell script of mine in the past.

 

me two, also have had times where I am trying to shell or execute something and use the wrong number of quotation marks and it throws it into invalid memory lol

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...