Solved

Find OU Path of Users and Export to LDAP

Posted on 2009-06-29
1
899 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

829 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