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

Generate sequential property values

February 27, 2024 Views: 1794

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.

PARAMETERS:

  • $numberProperty - the name of the property of the managed domain that stores the last used number.
  • $domainDN - Specifies the distinguished name (DN) of the domain that will store the last number set. For information on how to get the DN, see https://adaxes.com/sdk/HowDoI.GetDnOfObject/.
  • $propertyName - the name of the property to generate value for.
  • $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
$domainDN = "DC=domain,DC=com" # 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.TargetObject
    $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 domain property.
$domain = $Context.BindToObjectByDN($domainDN)

try
{
    $number = [int]($domain.Get($numberProperty))
    $number++
}
catch
{
    # 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 doamin property
$domain.Put($numberProperty, $number)
$domain.SetInfo()

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

Comments 0
Leave a comment
Loading...

Got questions?

Support Questions & Answers