The script checks whether the email address and username specified for a new user are unique. If either the username or the email address are not unique, new user creation will be cancelled. To use the script with Adaxes, create a business rule triggered before creating a user. For details, see Validate/Modify User Input Using a Script.
PowerShell
# Build criteria
if (-not([System.String]::IsNullOrEmpty("%mail%")))
{
$expression = {sAMAccountName -eq "%username%" -or mail -eq "%mail%"}
}
else
{
$expression = {sAMAccountName -eq "%username%"}
}
$criteria = New-AdmCriteria -Type "user" -Expression $expression
# Search for users with the username or email address specified
$searcher = $Context.TargetObject
$searcher.Criteria = $criteria
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.PageSize = 500
$searcher.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
$searcher.VirtualRoot = $True
$searcher.SizeLimit = 1
try
{
$searchResultIterator = $searcher.ExecuteSearch()
$searchResults = $searchResultIterator.FetchAll()
if ($searchResults.Length -ne 0)
{
$Context.Cancel("A user with the same username or email address already exists")
return
}
}
finally
{
# Release resources
$searchResultIterator.Dispose()
}
Yes, it is possible. You can use the $Context.SendMail method right after the following line in the script:
$Context.Cancel("A user with the same username or email address already exists")
Should you have any issues updating the script to meet your needs, please, describe the desired behavior in all the possible details with live examples.
Is there a way to modify the username from the already existing one to another username for the new user?
Like this: Already existing username ("pak") > new user ("pki")
Yes, it is possible. For details and examples, have a look at the following tutorial: https://www.adaxes.com/help/ValidateModifyUserInputWithScript.