Script Repository


Generate sequential property values

February 24, 2021
1107

The script generates sequential property values consisting of a fixed textual part and a sequence number, for example IDDQD-001, IDDQD-002, IDDQD-003 etc.

To generate object names using the script, create a business rule triggered before creating an object. For details, see Validate/Modify User Input Using a Script.

To improve the performance, the last used sequence number is stored in Adaxes configuration container. When generating a new value, the script gets the last number stored in the configuration container and increments it by 1. Nevertheless, the script also checks whether the value is unique in AD. If the generated value is not unique, the script will continue incrementing it until a unique value is found.

The number is stored in an Adaxes custom attribute of the configuration container, for example, CustomAttributeInt1. For information on how to check the last number used, see Get custom value stored in Adaxes configuration.

PARAMETERS:

  • $numberProperty - Specifies the LDAP name of the property of Adaxes configuration container that stores the next number to use.
  • $valueFormat - Specifies how to format the value. For details, see Getting started with the String.Format method.
  • $objectCategory - Specifies the object category for which values are generated, e.g. user or computer.
  • $initialNumber - Specifies the starting number to use if there is no number saved in Adaxes configuration.
  • $maxNumber - Specifies the maximum number that can be assigned.
Edit Remove
PowerShell
$numberProperty = "adm-CustomAttributeInt1" # TODO: modify me
$propertyName = "employeeID" # TODO: modify me
$valueFormat = "IDDQD-{0:000}" # TODO: modify me
$objectCategory = "user" # TODO: modify me
$initialNumber = 1 # TODO: modify me
$maxNumber = 999 # TODO: modify me

function IsValueNotUnique($filter)
{
    $searcher = $Context.BindToObject("Adaxes://rootDSE")
    $searcher.SearchFilter = $filter
    $searcher.SizeLimit = 1
    $searcher.VirtualRoot = $True
    
    try
    {
        $searchResultIterator = $searcher.ExecuteSearch()
        $searchResults = $searchResultIterator.FetchAll()
        
        return $searchResults.Length -eq 1
    }
    finally
    {
        # Release resources
        if ($searchResultIterator){ $searchResultIterator.Dispose() }
    }
}

# Get the number stored in the global configuration
$adaxesSettingsPath = $Context.GetWellKnownContainerPath("ConfigurationSetSettings")
$adaxesSettings = $Context.BindToObject($adaxesSettingsPath)

try
{
    $number = [int]($adaxesSettings.Get($numberProperty))
    $number++
}
catch
{
    # If number is not set in the global configuration, use the initial number
    $number = $initialNumber
}

# Build value
$uniqueValue = [System.String]::Format($valueFormat, $number)

do
{
    if ($number -gt [int]$maxNumber)
    {
        $Context.Cancel("Cannot generate a new value for $propertyName because the maximum `
                allowed object number has been reached. Contact your system administrator.")
        return
    }
    
    $isValueNotUnique =  IsValueNotUnique "(&(objectCategory=$objectCategory)($propertyName=$uniqueValue))"

    if ($isValueNotUnique)
    {
        # If the value is already in use, generate a unique one
        $number++
        $uniqueValue = [System.String]::Format($valueFormat, $number)
    }
}
while ($isValueNotUnique)

# Update the number in global configuration
$adaxesSettings.Put($numberProperty, $number)
$adaxesSettings.SetInfo()

# Update property value
$Context.SetModifiedPropertyValue($propertyName, $uniqueValue)


Comments ( 0 )
No results found.
Leave a comment