Sign in to follow this  
Followers 0
VAADAdmin

Append number to Username

8 posts in this topic

I am trying to determine a way(best way) to append a number to a username in Active Directory. I want to search AD for a username and if it exists, append a "2" to the username. Then search AD if username2 exists and if it does append "3" and so on. I have it where it will determine if the username exists or does not exist but cannot figure out how to find if 2 exists. Have a feeling it is arrays but I am very weak there. I am currently bringing them up in msgbox to get them to work first. Below is the code snippet I am working with.

$samAccountName = StringLower($leftFirst & $leftLast)
            
            $objSamAccountName =ObjGet("LDAP://cn=" & $samAccountName & "," & $destinationOU)
            
            MsgBox(64,"",$objSamAccountName)
            
            If IsObj($objSamAccountName) Then
                MsgBox(0,"Error",$samAccountName & "already exists")
            Else
                MsgBox(0,"Error",$samAccountName & "does not exist")
            EndIf

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Maybe something like this..

$counter = 1
While _CheckAccount($samAccountName)
    $counter += 1
    $samAccountName &= $counter
WEnd

Func _CheckAccount($samAccountName)
    $objSamAccountName = ObjGet("LDAP://cn=" & $samAccountName & "," & $destinationOU)
    If IsObj($objSamAccountName) Then
        Return True
    Else
        Return False
    EndIf
EndFunc
Edited by Joon

Share this post


Link to post
Share on other sites

Thank you very much for the reply. It works just great in my script.

Share this post


Link to post
Share on other sites

A problem did arise that I am working on but maybe someone has a quick reply. A number is added to the user ID if it exist, such as jsmith then jsmith2. The problem I am having is if jsmith2 exists it will append a 3 such as jsmith23. I want it to remove the 2 and just append the 3.

Maybe something like this..

$counter = 1
While _CheckAccount($samAccountName)
    $counter += 1
    $samAccountName &= $counter
WEnd

Func _CheckAccount($samAccountName)
    $objSamAccountName = ObjGet("LDAP://cn=" & $samAccountName & "," & $destinationOU)
    If IsObj($objSamAccountName) Then
        Return True
    Else
        Return False
    EndIf
EndFunc

Share this post


Link to post
Share on other sites

A problem did arise that I am working on but maybe someone has a quick reply. A number is added to the user ID if it exist, such as jsmith then jsmith2. The problem I am having is if jsmith2 exists it will append a 3 such as jsmith23. I want it to remove the 2 and just append the 3.

NOT TESTED!

if _CheckAccount($samAccountName) then
    $counter = 1
    While _CheckAccount($samAccountName,$counter)
        $counter += 1
        $samAccountName &= $counter
    WEnd
endif

Func _CheckAccount($samAccountName,$counter = "")
    $objSamAccountName = ObjGet("LDAP://cn=" & $samAccountName & $counter & "," & $destinationOU)
    If IsObj($objSamAccountName) Then
        Return True
    Else
        Return False
    EndIf

EndFunc

Cheers

Kurt


__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

A problem did arise that I am working on but maybe someone has a quick reply. A number is added to the user ID if it exist, such as jsmith then jsmith2. The problem I am having is if jsmith2 exists it will append a 3 such as jsmith23. I want it to remove the 2 and just append the 3.

$counter = ""
If StringIsAlpha($samAccountName) Then
    $counter = 1
Else
    For $i = Stringlen($samAccountName) To 1 Step -1
        If StringIsDigit(StringMid($samAccountName,$i,1)) Then 
            $counter = StringMid($samAccountName,$i,1) & $counter
        Else
            $samAccountName = StringLeft($samAccountName,$i)
            ExitLoop
        EndIf
    Next
EndIf       

While _CheckAccount($samAccountName & $counter)
    $counter += 1
WEnd
$samAccountName &= $counter

Func _CheckAccount($samAccountName)
    $objSamAccountName = ObjGet("LDAP://cn=" & $samAccountName & "," & $destinationOU)
    If IsObj($objSamAccountName) Then
        Return True
    Else
        Return False
    EndIf
EndFunc

Not Tested.

Edited by Joon

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

A slight variation on /dev/null's suggestion.

if _CheckAccount($samAccountName) then
    $counter = 2
    While _CheckAccount($samAccountName,$counter)
        $counter += 1
    WEnd
    $samAccountName &= $counter
endif

Func _CheckAccount($samAccountName,$counter = "")
    $objSamAccountName = ObjGet("LDAP://cn=" & $samAccountName & $counter & "," & $destinationOU)
    If IsObj($objSamAccountName) Then
        Return True
    Else
        Return False
    EndIf

EndFunc

Edit: Changed $counter=1 to $counter=2 so that you dont end up with Name1 as your first numbered account name

Edited by improbability_paradox

Share this post


Link to post
Share on other sites

Perfect, thank you all once again for the fast and accurate responses. I have the script working now thanks to everyones help. I went with improbability_paradox's slight variation on /dev/null's suggestion.

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  
Followers 0