Active Directory Scripting – retrieve Organizational Unit
Reference page: Active Directory – Sample Scripts (Excel/VBA)
How to retrieve Organizational Unit
Description
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: "dev.env.com/Main/Client/Company01"
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 'Error-handling If boolRC Then sReturnMessage = "Failed to retrieve Organizational Unit | Query = " & sQuery End If