Script Repository


Update Custom Command parameter values based on unique property values

February 04, 2019
31

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.
Edit Remove
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 ($parameter -eq $NULL)
{
    $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()

Comments ( 0 )
No results found.
Leave a comment