Script Repository


Create object names in sequential order

February 14, 2019
905

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.

Parameters:

  • $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
PowerShell
$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)

try
{
    $number = [int]($adaxesSettings.Get($numberProperty))
    $number++
}
catch
{
    # 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)

do
{
    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.")
        return
    }
    
    # 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

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

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

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

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


Comments ( 0 )
No results found.
Leave a comment