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: 1519

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
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")

Comments 0
Leave a comment
Loading...

Got questions?

Support Questions & Answers