Solved

Find OU Path of Users and Export to LDAP

Posted on 2009-06-29
1
889 Views
Last Modified: 2012-05-07
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:

Domain.com
---Staff
-------IT
-----------Test Accounts
-------Business
---Guests
-------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.

Thanks.
0
Comment
Question by:b4sherx
1 Comment
 
LVL 1

Accepted Solution

by:
b4sherx earned 0 total points
ID: 24737704
Found a good resource here, but had to modify it to suit my needs:
http://www.microsoft.com/technet/scriptcenter/resources/qanda/oct04/hey1021.mspx

Here's what I ended up doing:

On Error Resume Next
 
Const ADS_SCOPE_SUBTREE = 2
 
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")
WScript.Quit
Else
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
 
objRecordSet.MoveFirst
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

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question