Jump to content
water

Active Directory UDF (II)

Recommended Posts

water
Posted (edited)

You could run the following script to test:

#include <AD.au3>

_AD_Open()
_AD_ErrorNotify(1)
$aResult = _AD_RecursiveGetMemberOfEX(@UserName, 10)
_ArrayDisplay($aResult)
_AD_Close()
Exit

Func _AD_RecursiveGetMemberOfEX($sObject, $iDepth = 10, $bListenherited = True, $bFQDN = True)

    If $iDepth = Default Then $iDepth = 10
    If $bListenherited = Default Then $bListenherited = True
    If $bFQDN = Default Then $bFQDN = True
    If _AD_ObjectExists($sObject) = 0 Then Return SetError(1, 0, "")
    If StringMid($sObject, 3, 1) <> "=" Then $sObject = _AD_SamAccountNameToFQDN($sObject) ; sAMAccountName provided
    $sObject = _AD_FixSpecialChars($sObject, 1, '"\/#+<>;=') ; the object needs to be unescaped (except a comma) for the LDAP query but the result might be escaped
    Local $iCount1, $iCount2
    Local $sField = "distinguishedName"
    If Not $bFQDN Then $sField = "samaccountname"
ConsoleWrite("Querying: " & $sObject & @CRLF)
    $__oAD_Command.CommandText = "<LDAP://" & $sAD_HostServer & "/" & $sAD_DNSDomain & ">;(member=" & $sObject & ");" & $sField & ";subtree"
    Local $oRecordSet = $__oAD_Command.Execute
    Local $aGroups[$oRecordSet.RecordCount + 1] = [0]
    If $oRecordSet.RecordCount = 0 Then Return $aGroups
    $oRecordSet.MoveFirst
    $iCount1 = 1
    Local $aTempMemberOf[1]
    Do
        $aGroups[$iCount1] = $oRecordSet.Fields(0).Value
        If $iDepth > 0 Then
            $aTempMemberOf = _AD_RecursiveGetMemberOfEX($aGroups[$iCount1], $iDepth - 1, $bListenherited, $bFQDN)
            If $bListenherited Then
                For $iCount2 = 1 To $aTempMemberOf[0]
                    $aTempMemberOf[$iCount2] &= "|" & $aGroups[$iCount1]
                Next
            EndIf
            _ArrayDelete($aTempMemberOf, 0)
            _ArrayConcatenate($aGroups, $aTempMemberOf)
        EndIf
        $iCount1 += 1
        $oRecordSet.MoveNext
    Until $oRecordSet.EOF
    $oRecordSet.Close
    If $bListenherited = False Then
        _ArraySort($aGroups, 0, 1)
        $aGroups = _ArrayUnique($aGroups, 0, 1)
    EndIf
    $aGroups[0] = UBound($aGroups) - 1
    Return $aGroups

EndFunc   ;==>_AD_RecursiveGetMemberOfEX

 

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×