Hightower Posted March 31, 2011 Posted March 31, 2011 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
mac_shinobi Posted March 31, 2011 Posted March 31, 2011 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 ??
Hightower Posted March 31, 2011 Author Posted March 31, 2011 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) 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.
mac_shinobi Posted March 31, 2011 Posted March 31, 2011 (edited) 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 Edited March 31, 2011 by mac_shinobi 1
Hightower Posted March 31, 2011 Author Posted March 31, 2011 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.
Hightower Posted March 31, 2011 Author Posted March 31, 2011 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!
LosOjos Posted March 31, 2011 Posted March 31, 2011 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! 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!
SYNACK Posted March 31, 2011 Posted March 31, 2011 Yea, watch for trailing spaces, they have messed many a powershell script of mine in the past.
mac_shinobi Posted March 31, 2011 Posted March 31, 2011 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
plexer Posted March 31, 2011 Posted March 31, 2011 I expect that is exactly what it is a non visible character that was being passed. Ben
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now