Jump to content
Sign in to follow this  

Need to check AD display name, independent of any user

Recommended Posts


User1 first name = John

user1 last name = James

username = 44590

User2 first name = John

user2 last name = Jamison

username = 33432

Our It policy dictates that only the first 2 digits of the last name are used in the "last name" field for AD. So I have a script that StringLeft($lastname, 2)

Our IT policy also dictates that both of the above users "username" is actually their employee ID.

Both users must exist in the same OU. Due to LDAP/AD limatation, I can't have 2 users in the same OU with the same display name, thus I need to append a 1 after john jamison.

My problem is, i don't know how to use the AD_ObjectExists function independent of checking a username. I need to check ONLY the "cn" or "displayname" fields without regard to what their usernames are.

This particular function would go something like this.

take first name and last name into var's.

remove all but first 2 digits of lastname

make $displayname = $varfirst & " " & $varlast

Being loop

$count = "1"

Lookup to see if $displayname exists (which in this case it does, being "john ja" from user1 above)

If $displayname exists then $displayname &= $count

lookup $displayname again now that $displayname really equals (john ja1)

If $displayname exists again then $displayname &= $count + 1

do this loop until $displayname does not exist

continue on in the script with $displayname now equaling "john ja1", or "john ja2" or "john ja3", etc

most of the components I can code, but i don't know how to lookup a displayname without tying it to a username. Because the usernames will always be different I don't care what their username is at all.

Share this post

Link to post
Share on other sites

I think I've got it. Here is my code.

Func _CheckDisplayName()
$exist = ""
$var1 = _AD_GetObjectsInOU("", "(&(objectcategory=person)(objectclass=user)(sn=" & $lastname & "))", 2, "sn") ; check AD for duplicate display name
If @error > 0 Then
    ; display name does not exist continue script
    ; display name exists so loop until it doesnt
    $g = 0
        $g = $g + 1
        $lastname &= $g
        $var2 = _AD_GetObjectsInOU("", "(&(objectcategory=person)(objectclass=user)(sn=" & $lastname & "))", 2, "sn") ; check AD for duplicate name
        If @error > 0 Then
            ; display name does not exist so ready to create user
            $exist = False
            ; display name still exists so restart loop
            $lastname = StringTrimRight($lastname, 1) ; reset $lastname
            $exist = True
    Until $exist = False Or $g > 8
If $g > 8 Then
    $error &= "002x002 " ; too many users
    _ExcelWriteCell($book, $error, $i, $data[0][1] + 1)
$displayname = $firstname & " " & $lastname
Endfunc ;==> _CheckDisplayName

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