Active Directory Scripting – retrieve Security Group

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

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

How to retrieve Security Group


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: ""
' Security Groups
'  Group01 - belongs to Company01

How to retrieve Active Directory Security Group (Excel/VBA source code)

Note that Security Group is an object that is stored within Organizational Unit therefore LDAP query should specify the exact location.

'Build LDAP query to get an object
sQuery = "LDAP:// cn=Group01, ou=Company01, ou=Client, ou=Main"
sQuery = sQuery & ",dc=" & dvDC(0) & ",dc=" & dvDC(1) & ", dc=" & dvDC(2)
'Execute query
On Error Resume Next
Set objUserAccount = GetObject(sQuery)
boolRC = (Err.Number <> 0)
On Error GoTo 0
If boolRC Then
    sReturnMessage = "Failed to retrieve Security Group | Query = " & sQuery
End If


Active Directory Schema Terminology

Group 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.