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:
- $commandName – Specifies the name of the custom command whose parameter will be updated;
- $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.
PowerShell
$commandName = "My Command" # TODO: modify me
$parameterName = "param-MyParam" # TOOD: modify me
$propertyName = "departmentNumber" # TOOD: modify me
function SearchObjects($filter, $properties, $baseObjectPath, $virtualRoot)
{
$searcher = $Context.BindToObject($baseObjectPath)
$searcher.SearchFilter = $filter
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.PageSize = 500
$searcher.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
$searcher.SetPropertiesToLoad($properties)
$searcher.VirtualRoot = $virtualRoot
try
{
$searchResultIterator = $searcher.ExecuteSearch()
$searchResults = $searchResultIterator.FetchAll()
return ,$searchResults
}
finally
{
# Release resources
if ($searchResultIterator){ $searchResultIterator.Dispose() }
}
}
# Search Custom Command
$customCommandsPath = $Context.GetWellKnownContainerPath("CustomCommands")
$searchResults = SearchObjects "(&(objectClass=adm-CustomCommand)(name=$commandName))" @() $customCommandsPath $False
if ($searchResults.Length -eq 0)
{
$Context.LogMessage("Custom Command with name '$commandName' not found.", "Warning")
return
}
elseif ($searchResults.Length -gt 1)
{
$Context.LogMessage("Found more than one Custom Command with the following name: '$commandName'.", "Warning")
return
}
$customCommandPath = $searchResults[0].AdsPath
# Get parameter
$command = $Context.BindToObject($customCommandPath)
$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
}
# Search users
$searchResults = SearchObjects "(&(sAMAccountType=805306368)($propertyName=*))" @($propertyName) "Adaxes://RootDSE" $True
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()
$parameterValues = @{} to $parameterValues = [ordered]@{}
if ($parameterValues.ContainsKey($value)) to if ($parameterValues.Contains($value))