Invalid syntax on moving users between OUs

Discussion in 'Scripting' started by Chad Austin, May 25, 2005.

  1. Chad Austin

    Chad Austin Guest

    Here is a copy of my script. I am getting an invalid syntax. I have placed a
    comment in all caps on that line. Please let me know where my error is. I am
    a novice scripter and I need help please.

    Also the input file will look like this (i think) correct me if I am wrong


    username,"ou=desktop,ou=state,dc=ad,dc=domain,dc=com"





    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextFile = objFSO.OpenTextFile ("c:\scripts\move2.txt", 1)

    Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    strArray = Split(strNextLine , ",")
    strUser = strArray(0)
    strOU = strArray(1)
    call moveuser
    Loop



    sub moveuser

    Const ADS_SCOPE_SUBTREE = 2

    Set objConnection = CreateObject("ADODB.Connection")
    Set objCommand = CreateObject("ADODB.Command")
    objConnection.Provider = ("ADsDSOObject")
    objConnection.Open "Active Directory Provider"
    objCommand.ActiveConnection = objConnection
    objCommand.CommandText = "SELECT adsPath,samAccountName FROM " & _
    "'LDAP://dc=ad,dc=domain,dc=com' " & _
    "WHERE samAccountName = " & chr(39) & strUser & chr(39)
    objCommand.Properties("SearchScope") = ADS_SCOPE_SUBTREE
    Set objRecordSet = objCommand.Execute

    adspathUser=objRecordSet.Fields("adspath").Value


    Set objOU = GetObject(strOU) ' HERE IS WHERE I AM GETTING A SYNTAX ERROR
    Please HELP

    objOU.MoveHere adspathUser, vbNullString

    end sub
     
    Chad Austin, May 25, 2005
    #1
    1. Advertisements

  2. Hi,

    The variable strOU has the value
    "ou=desktop,ou=state,dc=ad,dc=domain,dc=com", which is the distinguished
    name. You need to append "LDAP://" to this when you bind to the object. For
    example:

    Set objOU = GetObject("LDAP://" & strOU)

    I haven't analyzed the rest of the script.
     
    Richard Mueller [MVP], May 26, 2005
    #2
    1. Advertisements

  3. Chad Austin

    Chad Austin Guest

    I tried that but it was also unsuccessful with the same error.

    Chad
     
    Chad Austin, May 27, 2005
    #3
  4. You cannot delimit the fields in your input file with commas if you have
    distinguished names. Given your example line, the Split function will break
    it up into "username", "ou=desktop", "ou=state", "dc=ad", etc.

    I would suggest delimiting with semicolons. For example:

    username;ou=desktop,ou=state,dc=ad,dc=domain,dc=com

    Then, use:
    strArray = Split(strNextLine, ";")
     
    Richard Mueller [MVP], May 28, 2005
    #4
  5. Hi,

    Took me awhile before I could test the rest of the script.

    1. Your subroutine binds to the Command and Connection objects repeatedly
    for each user. It would be more efficient to bind to these objects once in
    the main program, where they are declared with global scope (and are thus
    available in the sub).

    2. You cannot bind to strOU. This is the Distinguished Name of the OU, not
    the AdsPath. You must append "LDAP://". For example:

    Set objOU = GetObject("LDAP://" & strOU)

    This makes the code look like this:
    ===================
    Option Explicit

    Dim objFSO, objTextFile, strNextLine, strArray, strUser, strOU
    Dim objConnection, objCommand

    Const ADS_SCOPE_SUBTREE = 2

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    ' Set objTextFile = objFSO.OpenTextFile ("c:\scripts\move2.txt", 1)
    Set objTextFile = objFSO.OpenTextFile ("c:\scripts\move2.txt", 1)

    Set objConnection = CreateObject("ADODB.Connection")
    Set objCommand = CreateObject("ADODB.Command")
    objConnection.Provider = ("ADsDSOObject")
    objConnection.Open "Active Directory Provider"
    objCommand.ActiveConnection = objConnection
    objCommand.Properties("SearchScope") = ADS_SCOPE_SUBTREE

    Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    strArray = Split(strNextLine , ";")
    strUser = strArray(0)
    strOU = strArray(1)
    call moveuser(strUser, strOU)
    Loop

    sub moveuser(strUser, strOU)
    Dim objRecordset, adspathUser, objOU

    objCommand.CommandText = "SELECT adsPath FROM " & _
    "'LDAP://dc=Hilltop,dc=rlmueller,dc=net' " & _
    "WHERE samAccountName = '" & strUser & "'"

    Set objRecordSet = objCommand.Execute

    adspathUser = objRecordSet.Fields("adspath").Value

    Set objOU = GetObject("LDAP://" & strOU)

    objOU.MoveHere adspathUser, vbNullString

    end sub
    ===============
     
    Richard Mueller [MVP], May 28, 2005
    #5
  6. Chad Austin

    Chad Austin Guest

    I actully have it working with the Commas as my delimiters. I will look over
    your script and see the changes you have made, and I will also check into the
    issue of binding for every user.

    If you are interested in seeing the code how I have it working using the
    commas please let me know.

    Thank you for the help and information.

    Chad Austin
     
    Chad Austin, May 29, 2005
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.