Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Organizational Units in Active Directory?

Status
Not open for further replies.

rianeiromiron

Programmer
Jul 1, 2001
151
GT
Hello all:

I've been knocking my head for several days and can not find a solution for this.

I have several users which belong to an Organizational Unit in Active Directory. When they log into de domain, I am able (through a VFP program) to determine the name of the user, but I am not able to find the OU the they belong to. I, also, am able to list the Organizational Units in the domain (just in case).

Can anyone help me find how to find a user's OU?

Thanks to all.
 
HI,
Copy the following into a prg then pass a user name to it. It will return a string with that users ou etc.
If you named the prg GetDirInfo.prg you could do the following at foxpro command line: ?GetDirInfo("MyUser")
In return you will get a string with the info wanted. This assumes you are in their domain. But play with this code as it can do much more.




LPARAMETERS lcUserName

conn = CREATEOBJECT("ADODB.Connection")
rs = CREATEOBJECT("ADODB.Recordset")
conn.Open("Data Source=Active Directory Provider;Provider=ADsDSOObject")
oRoot = GetObject("LDAP://rootDSE")

sDomain = oRoot.Get("defaultNamingContext")
oDomain = GetObject("LDAP://" + sDomain)

rs = conn.Execute("SELECT * FROM '"+oDomain.AdsPath+"' WHERE CN='"+lcUserName+"'")
rs.movefirst
RETURN rs.Fields(0).Value
** END

Regards,

Rob
 
Here is the same program but I added a couple lines to trap for invalid user in domain:

LPARAMETERS lcUserName

loConn = CREATEOBJECT("ADODB.Connection")
loRs = CREATEOBJECT("ADODB.Recordset")
loConn.Open("Data Source=Active Directory Provider;Provider=ADsDSOObject")
loRoot = GetObject("LDAP://rootDSE")

lcDomain = loRoot.Get("defaultNamingContext")
loDomain = GetObject("LDAP://" + lcDomain)

loRs = conn.Execute("SELECT * FROM '"+loDomain.AdsPath+"' WHERE CN='"+lcUserName+"'")

IF !loRs.eof
loRs.movefirst
RETURN loRs.Fields(0).Value
ELSE
RETURN "User not found"
ENDIF

RELEASE loConn
RELEASE loRs
RELEASE loRoot
RELEASE loDomain

Regards,

Rob
 
Thank you Rob, but sorry. I've been trying the whole morning to get the code running, but LORS just does not populate. (by the way, I am going to need a little explanation on de SQL stuff, what is that Query?)

Good approach, though. Thank you.
 
Is the development computer you are running this query on 'joined' to the domain? You need to be joined to run the query.

Regards,

Rob
 
Is 'joined' a property of an object, or the fact that the developing machine is logged in the AD domain? The developing machine (my machine) is in the domain, so is my user.
 
I am using windows xp and visual foxpro 9. How about you? What OS and FoxPro version?

Regards,

Rob
 
Try this:

LPARAMETERS lcUserName

loConn = CREATEOBJECT("ADODB.Connection")
loRs = CREATEOBJECT("ADODB.Recordset")
loConn.Open("Data Source=Active Directory Provider;Provider=ADsDSOObject")
loRoot = GetObject("LDAP://rootDSE")

lcDomain = loRoot.Get("defaultNamingContext")
loDomain = GetObject("LDAP://" + lcDomain)

loRs = loconn.Execute("SELECT * FROM '"+loDomain.AdsPath+"' WHERE CN='"+lcUserName+"'")

IF !loRs.eof
loRs.movefirst
RETURN loRs.Fields(0).Value
ELSE
RETURN "User not found"
ENDIF

RELEASE loConn
RELEASE loRs
RELEASE loRoot
RELEASE loDomain

Regards,

Rob
 
Again, thank you Rob.

1) I used the last code you posted, however I get a User Not Found return with all users, except, if the user is the administrator. Can you think of something about this, or, do I have to be logged as an administrator of the domain to get the information.

2) Can you guide into the inners of the Execute("SELECT...."), I have no idea what that is (I mean, inside the ADODB)

Thank you
 
When you do ctrl-alt-del what does it say you are logged in as? "User X logged in as Domain\Username" Are you a member of domain admins? Try the following using no parameter and look at the cursor returned. Adodb returns recordsets which are stored in memory. Think of it as a cursor you can't view but can go through one line at a time and stuff the fields into a cursor as demonstrated below.
Try setting anonyomous queries on:

Intro to ldap:

MSDN link with lots of info:



I modified the version below to insert found data into a cursor rather than simply return it. You can do it with no parameter to get all data then inspect it. ie =test() or =test("")




Lparameters lcUserName

loConn = Createobject("ADODB.Connection")
loRs = Createobject("ADODB.Recordset")
loConn.Open("Data Source=Active Directory Provider;Provider=ADsDSOObject")
loRoot = Getobject("LDAP://rootDSE")

lcDomain = loRoot.Get("defaultNamingContext")
loDomain = Getobject("LDAP://" + lcDomain)

If !Empty(lcUserName)
loRs = loConn.Execute("SELECT * FROM '"+loDomain.AdsPath+"' WHERE CN='"+lcUserName+"'")
Else
loRs = loConn.Execute("SELECT * FROM '"+loDomain.AdsPath+"'")
Endif
lcString = ""
loRs.movefirst
If !loRs.Eof
Create Cursor Cactdir(Field1 C(200))
Do While !loRs.Eof
m.Field1 = lcString + loRs.Fields(0).Value
Insert Into Cactdir From Memvar
loRs.MoveNext
Enddo
Else
Messagebox("User not found")
Endif

Release loConn
Release loRs
Release loRoot
Release loDomain

Regards,

Rob
 
OOPS, try this one:

Lparameters lcUserName

loConn = Createobject("ADODB.Connection")
loRs = Createobject("ADODB.Recordset")
loConn.Open("Data Source=Active Directory Provider;Provider=ADsDSOObject")
loRoot = Getobject("LDAP://rootDSE")

lcDomain = loRoot.Get("defaultNamingContext")
loDomain = Getobject("LDAP://" + lcDomain)

If !Empty(lcUserName)
loRs = loConn.Execute("SELECT * FROM '"+loDomain.AdsPath+"' WHERE CN='"+lcUserName+"'")
Else
loRs = loConn.Execute("SELECT * FROM '"+loDomain.AdsPath+"'")
Endif

If !loRs.Eof
loRs.movefirst
Create Cursor Cactdir(Field1 C(200))
Do While !loRs.Eof
m.Field1 = loRs.Fields(0).Value
Insert Into Cactdir From Memvar
loRs.MoveNext
Enddo
Else
Messagebox("User not found")
Endif



Release loConn
Release loRs
Release loRoot
Release loDomain

Regards,

Rob
 
Yes, I am a member of the Domain Admins. and I am logged as ME. In the following hours I will try to digest all the info you sent. Thank you and talk to you later.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top