0 votes

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 remain a maximum of 8 characters long (%firstname:lower,1%%lastname:lower,7%). We would like a script that would remove a character first and then add number to make the make it unique and keep it at 8 characters or below.

For example; Alex Johnson is "ajohnson". Our current script will create "ajohnson1" if a duplicate is found. We would like the script to create "ajohnso1"

Here is our current script.

Import-Module Adaxes
function IsUserNameUnique($username)
{
   $user = Get-AdmUser $username -erroraction silentlycontinue
   return $user -eq $Null
}
# Get the username
$username = $Context.GetModifiedPropertyValue("samAccountName")
# Check if the username is unique
if (IsUserNameUnique($username))
{
    return
}
# If the username is not unique, generate a unique one
$uniqueUsername = $Null
for ($i = 1; $True; $i++)
{
    $uniqueUsername = $username + $i;
    if (IsUserNameUnique($uniqueUsername))
    {
        break
    }
}

# Update User Logon Name (pre-Windows 2000)
$Context.SetModifiedPropertyValue("samAccountName", $uniqueUsername)

# Update User Logon Name
$upnSuffix = $Context.GetObjectDomain("%distinguishedName%")
$userLogonName = $uniqueUsername + "@" + $upnSuffix
$Context.SetModifiedPropertyValue("userPrincipalName", $userLogonName)
$Context.LogMessage("The username has been changed to " + $userLogonName `
  + ".", "Information")

Thanks for the assistance.

by (520 points)

1 Answer

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

Hello,

See section Username has a length limitation in the following Script Repository Entry: http://www.adaxes.com/script-repository ... engthLimit.

Related questions

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 (550 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
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

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 ... first name backwards. Like Jdoe, oJDoe, hoJDoe. What can I do to fix this?

asked Nov 18, 2015 by lasership (370 points)
3,589 questions
3,278 answers
8,303 comments
548,130 users