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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with getting OU from active directory 1

Status
Not open for further replies.

zarkon4

MIS
Dec 16, 2003
641
0
0
US
I am needing to get the OU name using either the Windows user name or the computer name (preferebly computer name).
I would like to be able to get this info from ActiveDs.DLL
but finding documentation on it is very difficult.
How do I retrive this information from within a VB6 app?
 
That just gives me the computer name and the user name.
I need the Active Directory Organizational Unit (OU) from active directory using the computer name.
 
You might want to look in the Object Browser in the IDE... look at ActiveDs. I think you should be able to just use something like:

IADsOU.Name


Kevin
 
Thanks Smokin

I did get the result I was looking for through an alternate method as below.

Public Function GetOUName(strComputerName As String) As String
Dim adoConn As ADODB.Connection
Dim adoRs As ADODB.Recordset
Dim strSql As String
Dim iOUPos As Integer
Dim strOUName As String

Set adoConn = New ADODB.Connection

adoConn.Provider = "ADsDSOObject"
adoConn.Properties("User ID") = "Usr"
adoConn.Properties("Password") = "Pwd"
adoConn.Properties("Encrypt Password") = True
adoConn.Open "Active Directory Provider", "Usr", "Pwd"

Set adoRs = New ADODB.Recordset

'The following will return a string such as
'CN=MACHINE137,OU=Main Building,OU=Shop,DC=DOMAIN,DC=com
strSql = "SELECT distinguishedName FROM " & _
"'LDAP://DOMAIN' where objectCategory='computer' AND CN='" & _
strComputerName & "'"

On Error GoTo ErrorRoutine

adoRs.Open strSql, adoConn, 1, 1

iOUPos = InStr(1, adoRs!distinguishedName, "OU=")

strOUName = Mid$(adoRs!distinguishedName, iOUPos + 3)

GetOUName = Mid$(strOUName, 1, InStr(1, strOUName, ",") - 1)

adoRs.Close

Set adoRs = Nothing

adoConn.Close
Set adoConn = Nothing

Exit Function
ErrorRoutine:

GetOUName = strComputerName

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top