I know this post is old but, I am running into a new issue with building accounts. So here is the issue we a username that already exists in the system it will not add the next character in the name but the same character. For example we already have a username JohnsonS for Steve Johnson when we try to build Sue Johnson the script makes the username JohnsonSs when we really need to make it JohnsonSu. Any ideas on the issue would be helpful. Thanks
Import-Module Adaxes
$upnSuffix = "site.com" # TODO: modify me
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
$firstName = $Context.GetModifiedPropertyValue("givenName")
$lastName = $Context.GetModifiedPropertyValue("sn")
$uniqueUsername = $Null
if (($firstName -ne $NULL) -and ($lastName -ne $NULL))
{
$username = $lastName.Substring(0, 7)
$Context.LogMessage($username, "Information")
$Context.LogMessage($lastName, "Information")
foreach ($char in $firstName.ToCharArray())
{
$username = "$username$char"
if (IsUserNameUnique($username))
{
$uniqueUsername = $username
break
}
}
}
if ($uniqueUsername -eq $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
$Context.SetModifiedPropertyValue("userPrincipalName", "$uniqueUsername@$upnSuffix")
$Context.LogMessage("The username has been changed to " + $uniqueUsername `
+ ".", "Information")
$cas = "cas_{"
# Update User mailNickname NOT WORKING
$Context.SetModifiedPropertyValue("mailNickname", $cas)