Question

  • Creator
    Topic
  • #2238013

    AD User Creation Script Question

    Locked

    by yon.ubago ·

    Hello! I found a script yesterday on this site that creates users in AD from a csv file. Before I added 4 more entries for description,login script, home directory, and homedrive I was getting a loop detected. After adding the entries I get an “unterminated string constraint on line 40 (the “oNewUser.put “description?,sDescription” line).

    Any thoughts would be greatly appreciated!

    Here is the script:
    Dim sCSVFile
    Dim oConnection
    Dim oRecordSet
    Dim oNewUser

    ‘ Variables needed for LDAP connection
    Dim oRootLDAP
    Dim oContainer

    ‘ Holding variables for information import from CSV file
    Dim sLogon
    Dim sFirstName
    Dim sLastName
    Dim sDisplayName
    Dim sPassword
    Dim nPwdLastSet
    Dim nUserAccountControl ‘ Used to enable the account
    Dim sDomain
    Dim sDescription
    Dim sHomeDrive
    Dim sHomeFolder
    Dim sLoginScript

    ‘ Modify this to match your company’s AD domain
    sDomain=”foobar.local”

    ‘ Input file location
    sCSVFileLocation = “C:\Scripts\” ‘KEEP TRAILING SLASH!

    ‘ Full path to input file
    sCSVFile = sCSVFileLocation&”Book2.csv”

    ‘ Commands used to open the CSV file and select all of the records
    set oConnection = createobject(“adodb.connection”)
    set oRecordSet = createobject(“adodb.recordset”)
    oConnection.open “Provider=Microsoft.Jet.OLEDB.4.0;Data Source= ” & sCSVFileLocation & “;Extended Properties=””text;HDR=NO;FMT=Delimited”””
    oRecordSet.open “SELECT * FROM ” & sCSVFile ,oConnection

    ‘ Create a connection to the Active Directory Users container.
    Set oRootLDAP = GetObject(“LDAP://rootDSE”)
    Set oContainer = GetObject(“LDAP://cn=Users,” & _
    oRootLDAP.Get(“defaultNamingContext”))

    ‘ Allows processing to continue even if an error occurs (i.e. dup user)
    ‘ We put this below the CSV and AD information since processing can
    ‘ continue with a single bad record, but not if there is a problem with
    ‘ the CSV file or AD connection
    on error resume next

    do until oRecordSet.EOF ‘ Reads the values (cells) in the sInputFile file.

    ‘ ——— Start creating user account
    ‘ Read variable information from the CSV file
    ‘ and build everything needed to create the account
    sLogon = oRecordSet.Fields.Item(0).value
    sFirstName = oRecordSet.Fields.Item(1).value
    sLastName = oRecordSet.Fields.Item(2).value
    sDisplayName = sLastName&”, “&sFirstName
    sPassword = oRecordSet.Fields.Item(3).value
    sDescription = oRecordSet.Fields.Item(6).value
    sHomeDrive = oRecordSet.Fields.Item(7).value
    sHomeFolder = oRecordSet.Fields.Item(8).value
    sLoginScript = oRecordSet.Fields.Item(9).value

    ‘ Build the User account
    Set oNewUser = oContainer.Create(“User”,”cn=”&sFirstName&” “&SLastName)

    oNewUser.put “sAMAccountName”,lcase(sLogon)
    oNewUser.put “givenName”,sFirstName
    oNewUser.put “sn”,sLastName
    oNewUser.put “UserPrincipalName”,lcase(SLogon)&”@”&sDomain
    oNewUser.put “DisplayName”,sDisplayName
    oNewUser.put “name”,lcase(sLogon)
    oNewUser.put “description?,sDescription
    oNewUser.put “homeDrive?,sHomeDrive
    oNewUser.put “homeDirectory?,sHomeFolder
    oNewUser.put “scriptPath?,sLoginScript

    ‘ Write this information into Active Directory so we can
    ‘ modify the password and enable the user account
    oNewUser.SetInfo

    ‘ Change the users password
    oNewUser.SetPassword sPassword
    oNewUser.Put “pwdLastSet”, 0

    ‘ Enable the user account
    oNewUser.Put “userAccountControl”, 512
    oNewUser.SetInfo

    ‘ Used only for debugging
    ‘if err.number = -2147019886 then
    ‘ msgbox “User logon ” & sLogon & “already exists”
    ‘End If

    ‘ ——— End of user account creation

All Answers

  • Author
    Replies
    • #2615555

      Clarifications

      by yon.ubago ·

      In reply to AD User Creation Script Question

      Clarifications

    • #2615409

      Well, for one thing

      by neilb@uk ·

      In reply to AD User Creation Script Question

      Where’s the ‘Loop’ that closes the ‘do until’.

      Put it back! Put it back! 😀

      • #2606245

        In his defense….

        by barth.travis ·

        In reply to Well, for one thing

        …the script arrived without the loop at the end. I had to go through figuring that out too. 🙂

        -Doc

    • #2606249

      Same script, different problem

      by barth.travis ·

      In reply to AD User Creation Script Question

      I am running the same script, yet when I run it, only the first user is added and then the script hangs.

      Though the user is added, it does not activate the user account.

      So it seems to be dying somewhere in this stretch:

      ‘ Write this information into Active Directory so we can
      ‘ modify the password and enable the user account
      oNewUser.SetInfo

      ‘ Change the users password
      oNewUser.SetPassword sPassword
      oNewUser.Put “pwdLastSet”, 0

      ‘ Enable the user account
      oNewUser.Put “userAccountControl”, 512
      oNewUser.SetInfo

      If I comment out the setting of the password and last password change date, it still hangs and the account is still not activated.

      Thanks,

      -Doc

      • #2620423

        One thing that is also missing

        by neilb@uk ·

        In reply to Same script, different problem

        is

        oRecordset.Movenext

        You’re just stuck in a loop using the first record from the file. The line should be just above the “loop” that also isn’t there!

        • #2620264

          Thank you!

          by barth.travis ·

          In reply to One thing that is also missing

          I know NOTHING about scripting, or any kind of programming. I managed to figure out that I needed the LOOP, and how to add some more fields to the script, but I would have never figured out this bit of info.

          OK, I just tried it out and it now adds the user accounts. The only thing it does not seem to be doing is activating them. I can do that manually in just seconds, but it would be nice to see if I can get the script to do it.

          Man, this is great! Thanks everyone!

          -Doc

          EDIT:

          Can someone point me to a full list of the fields that I can pipe into a user account? Like sDisplayName and that stuff… I’d like to see a full list of those entries that I can populate with the script.

        • #2606627

          Two ways that I use

          by neilb@uk ·

          In reply to Thank you!

          Get hold of ADSIEDIT. This is a Microsoft mmc that will let you see every possible property including ones that you don’t have values for. Or you can get yourself an LDAP browser – Softerra do one and Google is your friend – and have a look at stuff that way.

          Other interesting stuff is the MS WMI Tools if you think that you want to be a scripting boy…

          Neil 😀

          Remember – with great power comes great responsibility. With the right script you can roger an entire AD senseless in seconds.

        • #2606626

          OK, one more tip

          by neilb@uk ·

          In reply to Thank you!

          oNewUser.AccountDisabled = False

          Wheeee and awa-a-a-ay we go.

        • #2922759

          if Possible

          by chris ·

          In reply to OK, one more tip

          this script sounds great.

          is it possible for someone to post a working version of this program, with a example of the CSV as I have tryed to read through the postings and I am not sure if this looping problem was fixed.

          also when I think about it, can this script create a Directory with a share and permissions for the use’s home drive ?

        • #2790575

          MoveNext

          by scottyfryer ·

          In reply to if Possible

          orecordset.movenext was what you need to add to move to the next record

    • #2948491

      Could you provide me the CSV file.

      by saidulu.p ·

      In reply to AD User Creation Script Question

      Hi,

      Could you provide me the CSV file.

      Thanks in advance,

    • #2819108

      I need model about CSV file plz

      by jereboss7 ·

      In reply to AD User Creation Script Question

      Hi,

      Can you give me a model csv file plz ?

      Thanks

Viewing 4 reply threads