0 votes

Hi,
Was wondering if there's a nice way to manually or automatically check if a username or an extension is in use before creating a user?

What do other people do? If there wasn't a nice way, I was thinking it might just be OK to follow the process of seeing an error in the results, deleting the user and starting again - but would rather the checks first.

by (120 points)

1 Answer

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

Hello Adam,

You can check whether the username is unique using a PowerShell script. If it is not unique, you can, for example, automatically add a digit to the end of the username. To learn how to do that, refer to the following tutorial: http://www.adaxes.com/tutorials_Simplif ... Script.htm. On Step 5, see Example 2.

If you prefer cancelling creation of a new user instead of adding a digit, that can also be done. For this purpose, use the following version of the script:

Import-Module Adaxes

$username = $Context.GetModifiedPropertyValue("samAccountName")

function IsUserNameUnique($username)
{
   $user = Get-AdmUser $username -erroraction silentlycontinue
   return $user -eq $Null
}
if (-not(IsUserNameUnique($username)))
{
    $Context.Cancel("The username is not unique.")
}

As for the extension, do you specify it using a certain property of the user account? If you do, you can use the same approach to check extensions specified for new users.

0

That's great thanks - I'm not sure what to replace $username with for the phone, as the options shown when pressing '$' don't relate to telephone. Can I call that field somehow to check?

0

Hello Adam,

Sure, here you are:

Import-Module Adaxes

function IsTelephoneNumberUnique($telephoneNumber)
{
   $user = Get-AdmUser -Filter 'telephoneNumber -eq $telephoneNumber' -erroraction silentlycontinue
   return $user -eq $Null
}

# Get the telephone number
$telephoneNumber = $Context.GetModifiedPropertyValue("telephoneNumber")

# Check if the telephone number is unique
if (-not(IsTelephoneNumberUnique($telephoneNumber)))
{
    $Context.Cancel("The telephone number is already in use")
}
0

Thanks, looks like the telephone is working but for the username one I get this error:

Cannot validate argument on parameter 'Identity'. The argument is null. Supply a non-null argument and try the command again.

This is the script, just copied/pasted:

Import-Module Adaxes
function IsUserNameUnique($username)
{
   $user = Get-AdmUser $username -erroraction silentlycontinue
   return $user -eq $Null
}
if (-not(IsUserNameUnique($username)))
{
    $Context.Cancel("The username is not unique.")
}
0

Hello Adam,

Sorry, there was a line missing. Here's the correct version of the script:

Import-Module Adaxes

$username = $Context.GetModifiedPropertyValue("samAccountName")

function IsUserNameUnique($username)
{
   $user = Get-AdmUser $username -erroraction silentlycontinue
   return $user -eq $Null
}
if (-not(IsUserNameUnique($username)))
{
    $Context.Cancel("The username is not unique.")
}
0

Thanks - tested and works perfectly now :)

0

Thanks! I was about to report this as well, but it now works perfectly!

Related questions

0 votes
1 answer

Hi all, I have a condition during new user creation - Where the corporate email is entered into the email address field, but a custom drop-down for "Mailbox required?" is No. ... screen, and be able to save the result of this choice to a variable? Thanks all,

asked Oct 24 by dshortall (80 points)
0 votes
1 answer

I used this script from the repository https://www.adaxes.com/script-repository/check-if-number-of-unused-microsoft-365-licenses-is-below-limit-s594.htm I have amended to include ... count is below what I specify. Please can you advise what I am doing wrong.

asked Jan 31 by MikeBeattie (110 points)
0 votes
1 answer

I've tried the following script to adapt the UPN to the country, the step will be processed in "before user creation" but the UPN stays ... # Save changes $Context.TargetObject.Put("userPrincipalName", $userPrincipalName) $Context.TargetObject.SetInfo()

asked Oct 12, 2022 by boris (530 points)
0 votes
1 answer

Hi Team, We are using a set of form/business rules/custom commands for user creation that move user account to the right OU given the Office attribute value. I would ... , but can't find valuable information on the web site. Thanks in advance Regards Stephen

asked Dec 12, 2012 by sroux (800 points)
0 votes
1 answer

We have a business need for automating and controlling the creation of service accounts in our AD. For example, we want all new service accounts to start with "svc_" for ... customize the "New User" form to create a "New Service Account" workflow in Adaxes?

asked Sep 10, 2021 by joshua.lapchuk (60 points)
3,549 questions
3,240 answers
8,232 comments
547,815 users