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

Update custom command parameter values based on unique property values

September 05, 2023 Views: 2212

The script finds all unique values of the given property for users and adds the values to a custom command parameter. The parameter should represent a list of values.

Parameters:

  • $commandDN – Specifies 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 – Specifies the name of the parameter to be updated with the param- prefix;
  • $propertyName – Specifies the LDAP name of the property (e.g. departmentNumber) whose unique values will be used for the parameter.
Edit Remove
PowerShell
$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 0
avatar
Eugene Oct 07, 2019
If you want the values to be ordered you can the lines
$parameterValues = @{} to $parameterValues = [ordered]@{}
if ($parameterValues.ContainsKey($value)) to if ($parameterValues.Contains($value))
Leave a comment
Loading...

Got questions?

Support Questions & Answers