Jump to content

active directory search


 Share

Recommended Posts

So here is the goal.

If I have 2 users with the names

Mike Johnsen

Mike Johntson

Because of security concerns we append the name to be first name and first 2 digits of last name

Mike Jo

Mike Jo

Obviously in active directory you can't have 2 users with the same display name. What I am trying to do is search active directory for display name. Check if display name already exists, if it does.. append a 1 after the name (or 2 or 3 or 4 depending how many duplicates there are)

Mike Jo

Mike Jo1

Mike Jo2

etc

The code I have built isn't working properly. Here is what I have so far.

; FormatNameProper
    $sFirst = _StringProper($sFirst)
    Local $sSurname = StringRegExpReplace($sLast, "[\W]", "") ; strip non word characters => O'Dell becomes ODell
    $sLast = _StringProper(StringLeft($sSurname, 2))
    ; /FormatNameProper

    ; CheckLastName
    Local $bExists = ""
    Local $iGet = _AD_GetObjectsInOU("", "(&(objectcategory=person)(objectclass=user)(displayName=" & $sFirst & " " & $sLast & "))", 2, "displayName") ; check AD for duplicate lastname
    ;(&(objectcategory=person)(objectclass=user)(sn=" & $sLast & "))"
    _ArrayDisplay($iGet)
    If @error > 0 Then
        ; display name does not exist
    Else
        ; append number to end of name and loop until display name doesnt exist
        $t = 0
        Do
            SetError(0)
            $t += 1
            $sLast &= $t
            $iGetAgain = _AD_GetObjectsInOU("", "(&(objectcategory=person)(objectclass=user)(sn=" & $sLast & "))", 2, "sn") ; check AD for duplicate lastname
            If @error > 0 Then
                ; display name does not exist so ready to create user
                $bExists = False
            Else
                ; display name still exists so restart loop
                $sLast = StringTrimRight($sLast, 1) ; delete changes made to $sLast
                $bExists = True
            EndIf
        Until $bExists = False Or $t > 8
    EndIf
    If $t > 8 Then _ExitWithError("Error creating : User " & $sUsername & " : too many display names")
    $sDisplayName = $sFirst & " " & $sLast
    ; /CheckLastName
    ConsoleWrite($sDisplayName & @CR)
Link to comment
Share on other sites

im not sure if it helps a lot but if you get the name and your willing to make it "mike jo 1" you could use StringSplit() and take the last array variable and add one to the number, and you could add a error checker to find if there is a number at the end.

as long as you can separate the number from the name it would work, so using somthing like "mike jo,1" instead of using a space would work

Example:

$array = StringSplit("Mike jo,1", ",")
$num = $array[2] + 1
$name = $array[1] & $num

this will result with $name equal to "Mike jo2"

if you want the "," in the name then its a simple change:

$name = $array[1] & "," & $num

Edit: added example

Edited by pieeater

[spoiler]My UDFs: Login UDF[/spoiler]

Link to comment
Share on other sites

I'm not sure that the displayname has to be unique. What has to be unique is the cn which is part of the FQDN.

Check this and this.

This MS article about uniqueness might help as well.

Even better:

"It should be noted that the sAMAccountName attribute of any object must be unique in the domain. The userPrincipalName must be unique in the forest. However, the cn attribute (common name) must only be unique in the container or organizational unit. There can be several objects with the same cn, as long as they are in different containers. Note, however, that the distinguishedName will always be unique in the forest."

You are talking about attribute "displayname" but in your query you use "sn" - so maybe you are mixing things up a bit.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

Right, display name just cant be the same in the same container. But we have duplicates in the same container so the need to append the numbers after the name still applies.

I was using surname originally, but noticed that any user with Jo was getting appended with a digit..

Jake Johnson

Mike Johnston

Larry Jones

the original script was doing

Jake Jo

Mike Jo1

Larry Jo2

So I need to look at the display name or cn.. either way.. I need this behavior

Jake Jo

Mike Jo

Larry Jo

Mike Jo1

Link to comment
Share on other sites

The logic of the script should look like this.

-take username (from an array)

Mike Jo

-search all of active directory for any display name that begins with "Mike Jo"

-if source displayname has a matching displayname in active directory then there must already be someone with that name.. so append a 1 to the name.

-loop to search active directory until a matching displayname does not exist (incrementing the 1 to a 2 to a 3 during each loop)

Link to comment
Share on other sites

well, I finally got it working. here is my code.

; FormatNameProper
    $sFirst = _StringProper(StringRegExpReplace($sFirst, "\A\s", "")) ; strip whitespace from beginning of name (genesis export issue)
    Local $sSurname = StringRegExpReplace($sLast, "[\W]", "") ; strip non word characters => O'Dell becomes ODell
    $sLast = _StringProper(StringLeft($sSurname, 2))
    $sDisplayName = $sFirst & " " & $sLast
    ; /FormatNameProper

    ; CheckLastName
    Local $aGet = _AD_GetObjectsInOU("", "(name=" & $sDisplayName & "*)", 2, "displayname")
    If @error > 0 Then
        ; display name does not exist
    Else
        ; append number to end of name and loop until display name doesnt exist
        $t = 0
        Do
            SetError(0)
            $t += 1
            $sLast &= $t
            $sDisplayName = $sFirst & " " & $sLast ; redefine $sDisplayName every loop to allow only last name to be altered
            Local $aGetAgain = _AD_GetObjectsInOU("", "(name=" & $sDisplayName & "*)", 2, "displayname")
            If @error > 0 Then
                ; display name does not exist so ready to create user
                $bExists = False
            Else
                ; display name still exists so restart loop
                $bExists = True
                $sLast = StringRegExpReplace($sLast, "\d", "") ; remove any digits from $sLast
            EndIf
        Until $bExists = False Or $t >= 9
        If $t >= 9 Then ConsoleWrite("error, too many names" & @CR)
    EndIf
    ; /CheckLastName
Link to comment
Share on other sites

  • 2 years later...

Hello Im new here, just wondering if anyone can help me provide the code to search a name within AD and display as array.

I want to understand more how autoit calls and connect to AD, as a basis just a simple code that ask for users input on a certain name and calls AD then display

Thank you

Link to comment
Share on other sites

This thread is 2 years old.

Please post your question on the >Help and Support thread of the AD UDF.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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...