Script Repository


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

June 19, 2018
361

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.BindToObject("Adaxes://rootDSE")
    $searcher.SearchFilter = "(cn=$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 )
No results found.
Leave a comment