CheckFieldUniqueness Component

Overview

The CheckFieldUniqueness method can be used to test the uniqueness of a targetEntity's field against the entities in the target entity space.

# field: string of the field
# entitiesToTest: collection of targetEntities
FieldUniquenessResult function CheckFieldUniqueness(field, entitiesToTest)

FieldUniquenessResult

The CheckFieldUniqueness returns an object called FieldUniquenessResult which contains the results of the uniqueness test. FieldUniquenessResult contains the following attributes:

Attributes Description Access Via
Uniques A collection of target entities that passed the uniqueness check $uniques = $result.Uniques;
LocalNonUniques A collection of target entities that failed the uniqueness check against another entity in the tested collection $localNonUniques = $result.LocalNonUniques;
ContextNonUniques A collection of target entities that failed the uniqueness check against an entity in the target entity space. $contextNonUniques = $result.ContextNonUniques;

Examples

$report = $components.CheckFieldUniqueness("uniqueId", $targetEntities);
# Entities with unique value for 'uniqueId' field
$uniqueEntities = $report.Uniques;
# Entities with a non-unique value for 'uniqueId' field compared against other entities in $targetEntities
$locallyNonUniques = $report.LocalNonUniques;
# Entities with a non-unique value for 'uniqueId' field compared against other entities already in the target entity space
$contextNonUniques = $report.ContextNonUniques;
# It is likely that the $sourceEntities would be tested for uniqueness
function Set-UniqueAccountName($entity, $retry)
{
    $first = $entity['GivenNames'].Value;
    $last = $entity['Surname'].Value -replace '[^A-Za-z]','';
    $id = "$first.$last";
    if ($retry -gt 0) { $id += $retry; }
    $entity['sAMAccountName'] = $id;
}
foreach ($entity in $targetEntities)
{
    Set-UniqueAccountName $entity 0;
}
$retry = 1;
$repeat = $true;
while ($repeat)
{
    $repeat = $false;
    $report = $components.CheckFieldUniqueness('sAMAccountName', $targetEntities);
    foreach ($entity in $report.ContextNonUniques)
    {
        Set-UniqueAccountName $entity $retry;
        $repeat = $true;
    }
    foreach ($entity in $report.LocalNonUniques)
    {
        Set-UniqueAccountName $entity $retry;
        $repeat = $true;
    }
    $retry += 1;
}

Is this article helpful for you?