Jump to content
Sign in to follow this  
kor

active directory search

Recommended Posts

kor

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)

Share this post


Link to post
Share on other sites
pieeater

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]

Share this post


Link to post
Share on other sites
water

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 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
kor

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

Share this post


Link to post
Share on other sites
kor

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)

Share this post


Link to post
Share on other sites
kor

anyone? I'm not making much progress.

Share this post


Link to post
Share on other sites
kor

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

Share this post


Link to post
Share on other sites
harbot

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

Share this post


Link to post
Share on other sites
water

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 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
Sign in to follow this  

×