Jump to content

Search Active Directory by attribute


Recommended Posts

I am using the following AD UDF script example to search our Active Directory by SamAccount/FQDN and it successfully returns the desired attributes associated with the AD object(s).  Recently, I am given a new requirement that now needs me to be able to search by an attribute instead of using SamAcount/FQDN.  I want to search using the "mail" attribute and need the return/output to spit back the "mailNickName" attribute.  How do I do it with the following example?

I know that I can user powershell to do this, but I prefer to keep the code in AutoIT.  For example, I can perform the same search in Powershell as follow.

$Get_SamAccount = Get-ADUser -Filter {mail -eq $email} | Format-Table SamAccountName

_AD_Open()
$aListProp = _AD_GetObjectProperties(@UserName, "mail")
_ArrayDisplay($aListProp)
msgbox(0,"", $aListProp[1][1])
_AD_Close()

 

 

Edited by LisHawj
Link to comment
Share on other sites

_AD_GetObjectsInOU is the function you are looking for. Specify the LDAP query to search for usres where mail=xxx and return mailnickname.
The examples that come with the UDF should explain how to do.
If not, please post here again :)
 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Thank you very much, Water!  Wow, literally wow!  Your comment solved my issue immediately upon reading your suggestion.  Again, thank you and I am providing both solutions I tested here in case another person or someone noob like myself ever needs to do the same thing I try today.

 

#Solution 1 - working, but it is too slow for my liking.
$sTempEID = GUICtrlRead($sEmail_ID) ;Read and capture employee email address.
If StringInStr($sTempEID, "@") Then
    If StringInStr($sTempEID, "@Learning123.com") Then ;Verify that the email string after @ is complete.
        ;Search Active Directory using the email format and return the mail attribute for the object.
        $iPID = Run(@ComSpec & " /c Powershell.exe Get-ADUser -Filter {mail -eq '" & $sTempEID & "'}", @ScriptDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
        ProcessWaitClose($iPID) ;Wait for the Powershell process to close.
        Local $sGet_SamAccount = StdoutRead($iPID) ;Read the output stream.
        $sGet_SamAccount = StringSplit(StringTrimRight(StringStripCR($sGet_SamAccount), 0), @CRLF) ;format the output string.
        For $i = 1 To UBound($sGet_SamAccount) - 1
            If StringInStr($sGet_SamAccount[$i], "SamAccountName") Then ;If SamAccountName is found then do the task below.
                $sTempEID = StringTrimLeft($sGet_SamAccount[$i], 20) ;Trim the unnecessary texts.
                ExitLoop
            EndIf
        Next
    EndIf
EndIf
$sEID = $sTempEID


#Solution 2 - native AutoIT and is FAST...like REALLY FAST!  I am using this one!
global $aObjects = _AD_GetObjectsInOU("", "(ANR=" & "MyName.LastName@Learning123.com" & ")", 2, "SAMAccountName", "")
If @error > 0 Then
    MsgBox(64, "Active Directory Functions - Example 3", "No objects found")
Else
    _ArrayDisplay($aObjects, "Active Directory Functions - Example 3  - Ambigous Name Resolution. Search for '" & "MyName.LastName@Learning123.com" & "'")
EndIf

 

Edited by LisHawj
Link to comment
Share on other sites

:)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...