Hello,
Here's the modified version of the script that should do the job:
if ($Context.IsPropertyModified("mail"))
{
# Get Email address
$mail = $Context.GetModifiedPropertyValue("mail");
# Check whether the email address is empty
if ([System.String]::IsNullOrEmpty($mail))
{
return
}
# Search all users
$searcher = New-Object "Softerra.Adaxes.Adsi.Search.DirectorySearcher" $NULL, $False
$searcher.SearchParameters.PageSize = 500
$searcher.SearchParameters.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.SearchParameters.Filter = "(&(objectCategory=user)(mail=$mail))"
$searcher.VirtualRoot = $True
$result = $searcher.ExecuteSearch()
$users = $result.FetchAll()
$result.Dispose()
# Check if the Email address is unique
if($users.Count -ne 0)
{
$Context.Cancel("Email address is already in use. Please verify that account is not being duplicated");
}
}
As to checking for unique SAMAccountNames, take a look at Example 2 in step 5 of the Validate/Modify User Input Using a Script Tutorial. It contains an example on how you can handle the task.
As to the effect of the $Context.Cancel line on a Business Rule that is triggered after user creation, the Business Rule will not be triggered. The Rule is set to be executed after user creation, and since $Context.Cancel cancels user creation, the event that should trigger the Rule will not occur.