Script repository

Update custom command parameter values with unique property values

Updated on: Jan 18, 2026, Views: 5598

Miscellaneous

The script finds all unique values of the specified property and sets the values as allowed for a custom command parameter. To execute the script, create a scheduled task configured for the Domain object type and add a managed domain to the Activity Scope of the task. The domain will only be used to trigger execution of the scheduled task.

Parameters

  • $commandDN – the distinguished name (DN) of the custom command whose parameter will be updated. For information on how to get the DN, see Get the DN of a directory object.
  • $parameterName – the name of the parameter to be updated. The name must include the param- prefix.
  • $propertyName – the name of the property (e.g. employeeID) whose unique values will be used for the parameter.
$commandDN = "CN=My command,CN=Custom Commands,CN=Configuration Objects,CN=Adaxes Configuration,CN=Adaxes" # TODO: modify me
$parameterName = "param-MyParam" # TOOD: modify me
$propertyName = "employeeID" # TOOD: modify me

# Bind to the custom command.
$command = $Context.BindToObjectByDN($commandDN)

# Get parameter
$parameters = $command.Parameters
$parameter = $parameters | Where {$_.Name -eq $parameterName}
if ($NULL -eq $parameter)
{
    $Context.LogMessage("Parameter '$parameterName' not found in Custom Command '$commandName'.", "Warning")
    return
}

# Build criteria
$criteria = New-AdmCriteria "user"
$simpleItem = $criteria.CreateSimple()
$simpleItem.SetProperty($propertyName).SetComparisonOperator("empty").AddValue($False)
$criteria["user"].Add($simpleItem)

# Search parameters
$searcher = $Context.TargetObject
$searcher.Criteria = $criteria
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.PageSize = 500
$searcher.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
$searcher.SetPropertiesToLoad(@($propertyName))
$searcher.VirtualRoot = $True

try
{
    # Execute search
    $searchResultIterator = $searcher.ExecuteSearch()
    $searchResults = $searchResultIterator.FetchAll()
    
    if ($searchResults.Length -eq 0)
    {
        $Context.LogMessage("No values found for the parameter '$parameterName'.", "Warning")
        return
    }
    
    $parameterValues = @{}
    foreach ($searchResult in $searchResults)
    {
        $value = $searchResult.Properties[$propertyName].Value        
        if ($parameterValues.ContainsKey($value))
        {
            continue
        }
        
        $parameterValue = $parameter.CreateValue()
        $parameterValue.Value = $value
        $parameterValues.Add($value, $parameterValue)
    }
    
    # Update custom command.
    $parameter.Values = $parameterValues.Values
    $command.Parameters = $parameters
    $command.SetInfo()
}
finally
{
    # Release resources
    if ($searchResultIterator){ $searchResultIterator.Dispose() }
}

Comments 1

You must be signed in to comment.

  • Eugene

    Eugene

    If you want the values to be ordered you can the lines

    $parameterValues = @{} 
    

    to

    $parameterValues = [ordered]@{}  
    

    and

    if ($parameterValues.ContainsKey($value)) 
    

    to

    if ($parameterValues.Contains($value))
    

Got questions?

Support Questions & Answers

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.