The script automatically generates a unique full name by adding a digit if the current value is not unique. To use the script, create a business rule triggering Before Creating a <object>. For details, see Validate/Modify User Input Using a Script.
PowerShell
function IsFullNameUnique($fullname)
{
$searcher = $Context.TargetObject
$searcher.Criteria = New-AdmCriteria -Type "*" -Expression {cn -eq $fullname}
$searcher.VirtualRoot = $True
$searcher.SizeLimit = 1
try
{
$searchResultIterator = $searcher.ExecuteSearch()
$searchResults = $searchResultIterator.FetchAll()
return $searchResults.Length -eq 0
}
finally
{
# Release resources
if ($searchResultIterator){ $searchResultIterator.Dispose() }
}
}
# Check if the full name is unique
if (IsFullNameUnique "%fullname%")
{
return
}
# If the full name is not unique, generate a unique one
$uniqueFullname = $Null
for ($i = 1; $True; $i++)
{
$uniqueFullname = "%fullname%" + $i
if (IsFullNameUnique $uniqueFullname)
{
break
}
}
# Rename the object
$Context.SetModifiedPropertyValue("name", $uniqueFullname)
$Context.LogMessage("Full Name has been changed to $uniqueFullname", "Information")