We use cookies to improve your experience.
By your continued use of this site you accept such use.
For more details please see our privacy policy and cookies policy.

Script Repository

Automatically add a digit to the full name if it is not unique in all domains

September 04, 2023 Views: 1346

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.

Edit Remove
function IsFullNameUnique($fullname)
    $searcher = $Context.TargetObject
    $searcher.Criteria = New-AdmCriteria -Type "*" -Expression {cn -eq $fullname}
    $searcher.VirtualRoot = $True
    $searcher.SizeLimit = 1
        $searchResultIterator = $searcher.ExecuteSearch()
        $searchResults = $searchResultIterator.FetchAll()
        return $searchResults.Length -eq 0
        # Release resources
        if ($searchResultIterator){ $searchResultIterator.Dispose() }

# Check if the full name is unique
if (IsFullNameUnique "%fullname%")

# If the full name is not unique, generate a unique one
$uniqueFullname = $Null
for ($i = 1; $True; $i++)
    $uniqueFullname = "%fullname%" + $i
    if (IsFullNameUnique $uniqueFullname)

# Rename the object
$Context.SetModifiedPropertyValue("name", $uniqueFullname)
$Context.LogMessage("Full Name has been changed to $uniqueFullname", "Information")

Comments 0
Leave a comment

Got questions?

Support Questions & Answers