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.