Active Directory Scripting – retrieve Organizational Unit

Posted by Albert Gareev on Feb 10, 2010 | Categories: Back-endSource code

Reference page: Active Directory – Sample Scripts (Excel/VBA)

How to retrieve Organizational Unit


Build LDAP Query – Execute Query – Catch Errors

Note. You need to login as an authorized person to successfully execute the script. 

Common definitions

' Domain: DEV.ENV.COM
Dim sDomain, dvDC
sDomain = "DEV.ENV.COM"
dvDC = Split(sDomain, ".")

' Organizational Units
'  Company01 - Active Directory Path: ""

How to retrieve Organizational Unit (Excel/VBA source code)

Note that we enlist directory tree nodes backwars when building LDAP Path, i.e. from branch to the root.

'Build LDAP query for "Company01"
sQuery = "LDAP:// ou=Company01, ou=Client, ou=Main"
sQuery = sQuery & ",dc=" & dvDC(0) & ",dc=" & dvDC(1) & ", dc=" & dvDC(2)

'Execute query
On Error Resume Next
Set objOU = GetObject(sQuery)
boolRC = (Err.Number <> 0)
On Error GoTo 0
If boolRC Then
    sReturnMessage = "Failed to retrieve Organizational Unit | Query = " & sQuery
End If


Active Directory Schema Terminology

Organizational-Unit Class

Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported
This work by Albert Gareev is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported.