0 votes

We have a process to create unique usernames but it doesn't account for hyphens. We would like to remove the hyphens and continue to use first initial plus the first seven characters from the last name. In a hyphen situation, we just want to continue to use the last name entered in the first position.

Example:

John Jones-Smith would be jjones instead of jjones-s.

Here's the code we are currently using.

function incusername
{
    $UNNum = 0
    $UNNAme = ""
    [int]$UNLT = $UN.ToString().Length
    If ($UNLT -gt 8)
    {
        exit
    }
    $dummy = $UN -match '\D+'
    $UNNAme = $matches[0].ToLower()
    If ($UN -match '\d+')
    {
        $UNNum = $matches[0]
        If ($UNNum -lt 9)
        {
            $UNName + ([int]$UNNum + 1)
        }
        Else
        {
            If ($UNNum -eq 9)
            {
                If ($UNLT -lt 8)
                {
                    $UNName = $UN.Substring(0, [math]::Min(($UNLT - 1), $UN.Length))
                    $UNName + ([int]$UNNum + 1)
                }
                Elseif ($UNLT -lt 7)
                {
                    $UNName + ([int]$UNNum + 1)
                }
                Else
                {
                    $UNName = $UN.Substring(0, [math]::Min(($UNLT - 2), $UN.Length))
                    $UNName + ([int]$UNNum + 1)
                }
            }
            Else
            {
                $UNName + ([int]$UNNum + 1)
            }
        }
    }
    Else
    {
        If ($UNLT -lt 8)
        {
            $UNName + ([int]$UNNum + 1)
        }
        Else
        {
            $UNName = $UN.Substring(0, [math]::Min(($UNLT - 1), $UN.Length))
            $UNName + ([int]$UNNum + 1)
        }
    }
}
by (20 points)
0

Disregard. Got it fixed.

Please log in or register to answer this question.

Related questions

0 votes
1 answer

We are currently using the script below to check for a unique username/upn. If the name is not unique, it adds a number to the end. In the Adaxes logs, everything appears ... .LogMessage("The username has been changed to " + $uniqueUPN ` + ".", "Information")

asked Feb 14 by cewilson (300 points)
0 votes
1 answer

Hi, I am looking for a solution which checks if the username ist unique with the following requirements: If [first letter of first name].[last name] is not possible as username ... letter of first name].[last name]2 I' am looking forward to a hint. Thanks

asked Feb 6, 2023 by boris (570 points)
0 votes
1 answer

Hello, I hope someone can help me with a specific script. I have tried to put 2 or 3 together that I have found on here but not having much luck. I am looking to have a ... -upn, but it doesn't seesm to be quite what I'm after. Any help would be appreciated.

asked May 20, 2020 by adantona (40 points)
0 votes
0 answers

Has anyone ever had the business requirement that the usernames of new users be unique across all of the managed domains in the environment? It is easy enough to run a ... the run as service account understand to look further into the other domains as well?

asked Jul 22, 2016 by strikk (360 points)
0 votes
1 answer

Hello, Currently we are using the script from another topic to add a number to the username counting up until it finds a unique name. However, we need the username to still ... changed to " + $userLogonName ` + ".", "Information") Thanks for the assistance.

asked Feb 9, 2016 by jhair (520 points)
3,648 questions
3,336 answers
8,432 comments
548,983 users