Checks before New User Creation

General discussion of using Adaxes for Active Directory management and administration
Post Reply
AdamFowlerIT
Posts: 7
Joined: Wed Feb 24, 2016 4:14 pm

Checks before New User Creation

Wed Feb 24, 2016 9:59 pm

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.

User avatar
Support
Site Admin
Posts: 2373
Joined: Thu Apr 23, 2009 2:28 am

Re: Checks before New User Creation

Mon Feb 29, 2016 5:01 am

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:

Code: Select all

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.
Active Directory Identity Management

Follow Adaxes in social networks
Image Image Image

AdamFowlerIT
Posts: 7
Joined: Wed Feb 24, 2016 4:14 pm

Re: Checks before New User Creation

Thu Mar 03, 2016 11:44 pm

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?

User avatar
Support
Site Admin
Posts: 2373
Joined: Thu Apr 23, 2009 2:28 am

Re: Checks before New User Creation

Fri Mar 04, 2016 9:09 am

Hello Adam,

Sure, here you are:

Code: Select all

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")
}
Active Directory Identity Management

Follow Adaxes in social networks
Image Image Image

AdamFowlerIT
Posts: 7
Joined: Wed Feb 24, 2016 4:14 pm

Re: Checks before New User Creation

Sun Mar 06, 2016 11:50 pm

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:

Code: Select all

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.")
}

User avatar
Support
Site Admin
Posts: 2373
Joined: Thu Apr 23, 2009 2:28 am

Re: Checks before New User Creation

Mon Mar 07, 2016 4:45 am

Hello Adam,

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

Code: Select all

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.")
}
Active Directory Identity Management

Follow Adaxes in social networks
Image Image Image

AdamFowlerIT
Posts: 7
Joined: Wed Feb 24, 2016 4:14 pm

Re: Checks before New User Creation

Mon Mar 07, 2016 3:44 pm

Thanks - tested and works perfectly now :)

Kikaida
Posts: 65
Joined: Thu Mar 14, 2013 9:31 pm

Re: Checks before New User Creation

Tue Mar 08, 2016 1:53 pm

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

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 3 guests