preload

Active Directory Scripting – find User Account

Posted by Albert Gareev on Mar 09, 2010 | Categories: Back-endSource code

 

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

Description 

The function below performs 1-layer deep search of User Account object in all Organizational Units contained under parent Organizational Unit. The function can be easily modified for a recursive search if children OU hierarchy is unknown. Use objUser.Get(“distinguishedName”) to retrieve full LDAP path of found User Account object and parse path string to get OU names that contain the object.

The function returns TRUE if search was successful.

(in) sLogin – is a unique user login name which must be same as CN of an object.

(in) objRootOU – root (parent) Organizational Unit.

(out) objUser – User Account object reference

 

 

Private Function SearchForUser(ByVal sLogin, ByRef objRootOU, ByRef objUser)
Dim boolFound
Dim objOU
Dim sOUName, sUserName

boolFound = False
sLogin = UCase(sLogin)

objRootOU.Filter = Array("organizationalUnit")
For Each objOU In objRootOU
    sOUName = Mid(objOU.Name, 4)
    objOU.Filter = Array("user")
    For Each objUser In objOU
        sUserName = Mid(objUser.Name, 4)
        If UCase(sUserName) = sLogin Then
            boolFound = True
            Exit For
        End If
    Next
   
    If boolFound Then
        Exit For
    End If
Next

SearchForUser = boolFound

End Function


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.