0 votes

Hello,

Im using a business rule triggered before a new user is created to check for username uniqueness. Our company continues to add characters from the first name to build out a unique user name. For example Jdoe, Jodoe, JohDoe,etc.

I'm using the script referenced here: Unique Username
This script appears to be designed to do something similar, except that the first name is at the end of the username.
I modified the script to:

if (($firstName -ne $NULL) -and ($lastName -ne $NULL))
{
    $username = $lastName
    foreach ($char in $firstName.ToCharArray())
    {
        $username = "$char$username"
        if (IsUserNameUnique($username))
        {
            $uniqueUsername = $username
            break
        }
    }
}

however, it builds the first name backwards. Like Jdoe, oJDoe, hoJDoe.

What can I do to fix this?

by (370 points)

1 Answer

0 votes
by (215k points)
selected by
Best answer

Hello,

This one should work:

if (($firstName -ne $NULL) -and ($lastName -ne $NULL))
{
    $firstNamePart = ""
    foreach ($char in $firstName.ToCharArray())
    {
        $firstNamePart = $firstNamePart + $char
        $username = "$firstNamePart$lastName"
        if (IsUserNameUnique($username))
        {
            $uniqueUsername = $username
            break
        }
    }
}

Related questions

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)
0 votes
0 answers

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 ... ($UNLT - 1), $UN.Length)) $UNName + ([int]$UNNum + 1) } } }

asked Dec 11, 2015 by tcarp (20 points)
0 votes
1 answer

When I build accounts on the WebUI I run the a script to check if the name is unique which works. But the issue is this script adds a 1 if the username is taken. ... .LogMessage("The username has been changed to " + $userLogonName ` + ".", "Information")

asked Jun 24, 2014 by 7efd721c8b (420 points)
2,801 questions
2,535 answers
6,605 comments
61,659 users