• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 916
  • Last Modified:

Find OU Path of Users and Export to LDAP

I am currently migrating users to a new SAN and am writing a script to automate most of the process. What I am running into is having to change the LDAP string to suit the user I am migrating (specifically OU's). Does anyone know of a way to search for an AD user and output the OU path to a LDAP string? We have a pretty deep OU "tree", so it would look something like this:

-----------Test Accounts
-------so on and so forth

I'm using vb script to code this and any help or push in the right direction would help out tremendously.

1 Solution
b4sherxAuthor Commented:
Found a good resource here, but had to modify it to suit my needs:

Here's what I ended up doing:

On Error Resume Next
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
FindUser = InputBox("Please Enter A UserName", "Find User OU")
If FindUser = "" Then
MsgBox("No UserName Was Added")
strUser = FindUser
End If
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.CommandText = _
    "SELECT distinguishedName FROM 'LDAP://dc=leeflames,dc=lan'WHERE objectCategory='user'AND sAMAccountName='" & strUser & "'"
Set objRecordSet = objCommand.Execute
strDN = objRecordSet.Fields("distinguishedName").Value
'Removed all the parsing and the loop
'    arrPath = Split(strDN, ",")
'    intLength = Len(arrPath(1))
'    'intNameLength = intLength - 3
'    Wscript.Echo Right(arrPath(1), intLength)
'Echoed out the full string for debugging    
Wscript.Echo strDN
Set objUser = GetObject("LDAP://" & strDN)

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now