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

Create object names in sequential order

The script creates object names consisting of a fixed textual part and a sequence number, for example PC-001, PC-002, PC-003 etc.

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

For better performance, the last used sequence number is stored in Adaxes configuration container. To create a new username, the script gets the last number stored in the configuration container and increments it by 1. Nevertheless, the script also checks whether the username is unique in AD. If the generated username is not unique, the script will continue incrementing the value until a unique one is created.

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.


  • $numberProperty - Specifies the property of Adaxes configuration container that stores the last used number.
  • $objectNameFormat - Specifies how to format the object name. For details, see Getting started with the String.Format method.
  • $objectCategory - Specifies the object category for which names 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.

See Also:

Edit Remove
$numberProperty = "adm-CustomAttributeInt1" # TODO: modify me
$objectNameFormat = "PC-{0:000}" # TODO: modify me
$objectCategory = "computer"
$initialNumber = 1 # TODO: modify me
$maxNumber = 999 # TODO: modify me

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

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

# Build new object name
$uniqueObjectName = [System.String]::Format($objectNameFormat, $number)

    if ($number -gt [int]$maxNumber)
        $Context.Cancel("Cannot generate a new object name because the maximum allowed `
                object number has been reached. Contact your system administrator.")
    # Check whether the name is in use
    $searcher = $Context.BindToObject("Adaxes://rootDse")
    $searcher.SearchFilter = "(&(objectCategory=$objectCategory)(sAMAccountName=$uniqueObjectName))"
    $searcher.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
    $searcher.VirtualRoot = $True

        $searchResultIterator = $searcher.ExecuteSearch()
        $searchResults = $searchResultIterator.FetchAll()

        # If the name is already in use, generate a unique one
        if ($searchResults.Count -ne 0)
            $uniqueObjectName = [System.String]::Format($objectNameFormat, $number)
		# Release resources
while ($searchResults.Length -ne 0)

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

# Update object name
$Context.SetModifiedPropertyValue("sAMAccountName", "$uniqueObjectName") # Computer or User Name (pre-Windows 2000)
$Context.SetModifiedPropertyValue("name", $uniqueObjectName) # Name / Full Name

Comments 0
Leave a comment

Got questions?

Support Questions & Answers