+ Post New Thread
Results 1 to 10 of 10
Coding Thread, VB.net - Head Frazzled in Coding and Web Development; Ok, my head is frazzled. I have some code in VB.net (2010) that is supposed to create a user. It ...
  1. #1

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    242

    VB.net - Head Frazzled

    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.

    Code:
    de.Invoke("Put", New Object() {"sAMAccountName", username})
    So can anyone tell me why this might be failing - my head is frazzled!

    Code:
    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

  2. #2

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,931
    Thank Post
    3,463
    Thanked 1,090 Times in 1,005 Posts
    Rep Power
    371
    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 ??

  3. #3

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    242
    I am passing the username variable, here

    Code:
    Private Sub AddUserToAD(ByVal username As String, 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.

  4. #4

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,931
    Thank Post
    3,463
    Thanked 1,090 Times in 1,005 Posts
    Rep Power
    371
    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
    Last edited by mac_shinobi; 31st March 2011 at 03:36 PM.

  5. Thanks to mac_shinobi from:

    Hightower (31st March 2011)

  6. #5

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    242
    Quote Originally Posted by mac_shinobi View Post
    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.

  7. #6

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    242
    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!

  8. #7

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,578
    Thank Post
    1,473
    Thanked 1,227 Times in 835 Posts
    Rep Power
    753
    Quote Originally Posted by Hightower View Post
    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!

  9. #8

    SYNACK's Avatar
    Join Date
    Oct 2007
    Posts
    11,247
    Thank Post
    882
    Thanked 2,745 Times in 2,319 Posts
    Blog Entries
    11
    Rep Power
    785
    Yea, watch for trailing spaces, they have messed many a powershell script of mine in the past.

  10. #9

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,931
    Thank Post
    3,463
    Thanked 1,090 Times in 1,005 Posts
    Rep Power
    371
    Quote Originally Posted by SYNACK View Post
    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

  11. #10

    plexer's Avatar
    Join Date
    Dec 2005
    Location
    Norfolk
    Posts
    13,630
    Thank Post
    734
    Thanked 1,692 Times in 1,506 Posts
    Rep Power
    434
    I expect that is exactly what it is a non visible character that was being passed.

    Ben



SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 2
    Last Post: 18th March 2011, 07:29 PM
  2. Hyper V going head to head with Virtual Server
    By Geek_of_HeathMount in forum Thin Client and Virtual Machines
    Replies: 6
    Last Post: 20th November 2009, 04:27 PM
  3. Replies: 23
    Last Post: 3rd October 2009, 06:44 PM
  4. Doing my head in...!
    By tech_guy in forum General Chat
    Replies: 3
    Last Post: 17th November 2008, 09:34 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •