The following version of the script can be used in Business Rules triggered both before a user is created and before a user is modified:
Import-Module Adaxes
# Get Company
if ($Context.IsPropertyModified("company"))
{
$company = $Context.GetModifiedPropertyValue("company")
}
else
{
try
{
$company = $Context.TargetObject.Get("company")
}
catch
{
$company = $NULL
}
}
if ($company -eq $NULL)
{
$Context.Cancel("No company specified.")
return
}
# Check if Initials were modified
if ($Context.IsPropertyModified("initials"))
{
$initials = $Context.GetModifiedPropertyValue("initials")
}
else
{
try
{
$initials = $Context.TargetObject.Get("initials")
}
catch
{
$initials = $NULL
}
}
if ($initials -eq $NULL)
{
return
}
$users = Get-AdmUser -Filter {(initials -eq $initials) -and (company -eq $company)}
if ($users -ne $NULL)
{
# Get usernames of all users who have the same initials and company
$Context.LogMessage("The following users also have the same initials:", "Warning")
foreach ($user in $users)
{
$Context.LogMessage($user.Name, "Warning")
}
$Context.Cancel("Ein Benutzer mit diesen Initialen existiert bereits!")
}
Please pay attention that when you make changes via the Administration Console, and change both the properties simultaneously, Adaxes will save the changes as two separate operations, setting the properties one-by-one. That is, the Business Rule will be triggered twice: first when the Initials property is updated, and then when the Company property is updated, or vice versa.
This behavior is observed only in the Administration Console. In the Web interface, if you change both the properties simultaneously, they will be set within one operation.