0 votes

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 then

  1. Try full name:

[first name].[last name]

if not possbile try

  1. middle name:

[first name].[other given name].[last name]

if not possbile try

  1. last name only:

[last name]

if not possbile try

  1. add number

[first letter of first name].[last name]2

I' am looking forward to a hint. Thanks

by (530 points)

1 Answer

0 votes
by (289k points)

Hello Boris,

Have a look at the following tutorial: https://www.adaxes.com/help/ValidateModifyUserInputWithScript. Also, the following script from our repository might be helpful: https://www.adaxes.com/script-repository/automatically-add-a-digit-to-the-username-if-it-is-not-unique-s298.htm. If you face issues writing the script yourself, please, specify the following:

  • What exactly do you mean by add number? Should it be done once or until the username becomes unique?
  • What should be done if a unique username cannot be formed?

Any additional details will be much appreciated.

0

Thanks for helping me! I've tried the tutorial but I'am not able to catch all 4 cases in a script.

The script should try the 4 alternative naming steps, one after the other, until the name becomes unique. If also step 4 is not possible then it should count up until the name gets unique:

[first letter of first name].[last name]2 [first letter of first name].[last name]3 [first letter of first name].[last name]4 .....

0

Hello Boris,

We will gladly help you with the script. In step 2 you specified middle name [first name].[other given name].[last name]. The template looks incorrect as first name and given name is the same thing. Should it be [first name].[initials].[last name]?

0

Sorry for the misunderstanding. It should be middle name

adaxess.JPG

Thanks!

0

Hello Boris,

Thank you for specifying. You can use the below script:

function IsUserNameUnique($username)
{
    $domain = $Context.GetObjectDomain("%distinguishedName%")
    $user = Get-AdmUser -Filter {sAMAccountName -eq $username} -Server $domain -AdaxesService localhost
    return $null -eq $user
}

function UpdateUser($username, $domain)
{
    $userPrincipalName = $username + $domain
    $Context.SetModifiedPropertyValue("sAMAccountName", $username)
    $Context.SetModifiedPropertyValue("userPrincipalName", $userPrincipalName)
}

# Get initial values
$username = $Context.GetModifiedPropertyValue("samAccountName")

# Check initial username for uniqueness
$usernameIsUnique = IsUserNameUnique $username
if ($usernameIsUnique)
{
    return
}

# Get domain part for UPN
$userPrincipalName = $Context.GetModifiedPropertyValue("userPrincipalName")
$localPart, $domainPart = $userPrincipalName.Split("@")
$domainPart = "@" + $domainPart

# Check second appoach
$username = "%firstname:lower%.%lastname:lower%"
$usernameIsUnique = IsUserNameUnique $username
if ($usernameIsUnique)
{
    UpdateUser $username $domainPart
    return
}

# Check third appoach
$username = "%firstname:lower%.%middleName:lower%.%lastname:lower%"
$usernameIsUnique = IsUserNameUnique $username
if ($usernameIsUnique)
{
    UpdateUser $username $domainPart
    return
}

# Check fourth appoach
$username = "%lastname:lower%"
$usernameIsUnique = IsUserNameUnique $username
if ($usernameIsUnique)
{
    UpdateUser $username $domainPart
    return
}

# If the username is not unique, add a digit to it.
$uniqueUsername = "%firstname:lower,1%.%lastname:lower%"
for ($i = 1;; $i++)
{
    $uniqueUsername = $uniqueUsername + $i
    if (IsUserNameUnique $uniqueUsername)
    {
        UpdateUser $uniqueUsername $domainPart
        break
    }
}
0

Thanks a lot for the script, works as expected! Great!

0

I have a follow-up question about this, what do I set as a variable in the RemoteRoutingAdressTemplate. So far I had entered here

%firstname:lower,1%.%lastname:lower%@$tenantname.mail.onmicrosoft.com"

Thanks

0

Hello Boris,

Sorry for the confusion, but we are not sure what exactly your question is about. There is no such variable in the script.

0

Sorry for the misunderstanding, I am talking about the RemoteRoutingAddress here:

$addressTemplate = %firstname:lower,1%.%lastname:lower%@$tenantname.mail.onmicrosoft.com"

https://www.adaxes.com/help/EnablingDisablingRemoteMailboxes/#change-remote-routing-address-template

0

Hello Boris,

Thank you for clarifying. That is where you need to specify a template for the remote routing address that will be used to automatically enable remote mailboxes. As it is mentioned in the variable description, you can use value references in the template. For each user they will resolve into the corresponding values of their account.

0

Thanks, but if we have the case the username was not uniqe and the script above changes the username to:

$uniqueUsername ="%firstname:lower%.%middleName:lower%.%lastname:lower%"

I also need this unique username as new variable for the remote routing adress template.

How can I catch this?

0

Hello Boris,

You can use the following approach:

$addressTemplate = %username%@company.mail.onmicrosoft.com"

As the script for unique username generation is executed before users are created, the value reference will result into the final username the account is actually created with.

0

OK now I get it, thanks.

0

It's not often that the script is used, so I'm just getting back to you. In case of the change to "%firstname:lower%.%lastname:lower%" (2nd approach) there is always the problem that the following business rules don't work anymore, because the UPN is then always "firstname.lastname@" without domain ending
In this case there seems to be an error in the script, can you reproduce this?Unbenannt.jpg****

0

Hello Boris,

Sorry for the confusion, but with the current screenshot we are not able to provide any insight on the issue. If you cannot post an uncovered screenshot here, please, send it to us at support@adaxes.com. Also, please, send a screenshot of the business rule itself.

Additionally, please, specify the version of Adaxes you are currently using (including the build number). For details on how to check it, see https://www.adaxes.com/help/CheckServiceVersion.

0

Hello Boris,

Thank you for the provided details. Unfortunately, there was a mistake in the script. To fix it, replace this line in the script

$localPart, $domainPart = $username.Split("@")

with the below one

$localPart, $domainPart = $userPrincipalName.Split("@")

We also update the script in the above post as well.

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

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,548 questions
3,239 answers
8,232 comments
547,814 users