Script Repository


Copy allowed property values from property pattern to custom command parameter

February 25, 2021
383

The script replaces values of a custom command drop-down list parameter with allowed property values from a property pattern. To execute the script, create a scheduled task configured for the Domain-DNS object type and add a managed domain to the Activity Scope of the task.

Parameters:

  • $propertyPatternDN - Specifies the distinguished name (DN) of the property pattern the list of allowed values will be copied from. For information on how to get the DN of a directory object, have a look at the following article: https://www.adaxes.com/sdk/HowDoI.GetDnOfObject.
  • $propertyName - Specifies the LDAP name of the property the list of allowed values will be copied from.
  • $customCommandDN - Specifies the distinguished name (DN) of the custom command whose parameter will be updated. For information on how to get the DN of a directory object, have a look at the following article: https://www.adaxes.com/sdk/HowDoI.GetDnOfObject.
  • $parameterName - Specifies the name (with the param- prefix) of the custom command parameter whose allowed values will be updated.

Edit Remove
PowerShell
$propertyName = "Department" # TODO: modify me
$propertyPatternDN = "CN=User Pattern,CN=Builtin,CN=Property Patterns,CN=Configuration Objects,CN=Adaxes Configuration,CN=Adaxes" # TODO: modify me
$customCommandDN = "CN=Change Department,CN=Custom Commands,CN=Configuration Objects,CN=Adaxes Configuration,CN=Adaxes" # TODO: modify me
$parameterName = "param-departmentName" # TOOD: modify me

# Bind to the Property Pattern
$propertyPattern = $Context.BindToObjectByDN($propertyPatternDN)

# Get allowed property values
$values = New-Object System.Collections.ArrayList
foreach ($item in $propertyPattern.Items)
{
    if ($item.PropertyName -ne $propertyName)
    {
        continue
    }
    
    $constraints =  $item.GetConstraints()
    try
    {
        $constraint = $constraints.GetConstraint("ADM_PROPERTYCONSTRAINTCATEGORY_VALUEFORMAT")
    }
    catch
    {
        $Context.LogMessage("No allowed values are specified for the '$propertyName' property in the property pattern.", "Warning")
        return
    }
    
    
    if ($constraint.Type -eq "ADM_PROPERTYCONSTRAINTTYPE_VALUERANGE")
    {
        # Get allowed values
        $constraint.Values | %%{[void]$values.Add($_)}
    }
    else
    {
        $Context.LogMessage("No allowed values are specified for the '$propertyName' property in the property pattern.", "Warning")
        return
    }
    
}

if ($values.Count -eq 0)
{
    $Context.LogMessage("No constraint is defined for the '$propertyName' property in the property pattern", "Warning")
    return # Property is not defined in the Property Pattern
}

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

# Update the list of allowed values
$parameterValues = @()
foreach ($value in $values)
{
    $parameterValue = $parameter.CreateValue()
    $parameterValue.Value = $value
    $parameterValues += $parameterValue
}

$parameter.Values = $parameterValues
$customCommand.Parameters = $parameters
$customCommand.SetInfo()

Comments ( 8 )
avatar
Robert Aich
Jan 12, 2022
Hello,
i'd like to extend these with additional item text.
Is there any possiblity to specify an item Text connected to values,
using formated string in property pattern with delimiter character?
e.g. Item_Text#Value

If yes, how will the PS code looks like for that?

thank you in advance
R.Aich
avatar
Support
Jan 12, 2022
Hello Robert,

Sorry for the confusion, but we are not sure what exactly you need to achieve. Please, describe the desired behavior in all the possible details with live examples.
avatar
Robert Aich
Jan 12, 2022
Hello,
i want to achive to have an specific "item text" for each entry inside the custom command drop down field and use a cryptic Value (like "comp1loc1" as value, which i want to use in a connected PSScript.

Inside a custom command i can define a parameter with a drop down list behind.
For these drop down list i am able to define hard values and item text optional, directly inside the parameter configuration.

The other way i want to use is your approach with "Copy allowed property values from property pattern to custom command parameter".
The parameters which should appear in this drop down list (Custom command) should filled out of defined property patterns.

In your script you get the Propterypattern Values and build with these the drop down list, but only the values no optional item text.

e.g. property pattern.
CustomAttributeText10 ---> Constraints -> must be one of the following values only
Location1#LocCode1
Location2#LocCode2


in PS script splitting up the pattern value first part to "item text", second part "value" (custom command drop down value)

Location1 -->ItemText for drop down entry
LocCode1 --> Value behind drop down entry
avatar
Support
Jan 12, 2022
Hello Robert,

Thank you for the provided details. Do we understand correctly that there is a part of the value specified in the property pattern that should be used as item text in the custom command parameter? If so, what is the separator? Could you, please, send us (support@adaxes.com) a screenshot of a drop-down defined in a property pattern along with a screenshot of the parameter items with the corresponding texts?
avatar
Robert Aich
Jan 12, 2022
Hello,
the separator will be, in my case, the"hash character" (#) to split the value string inside the PS Script
####
foreach ($value in $values)
{
$parameterValue = $parameter.CreateValue()
$parameterValue.Value = $value
$parameterValues += $parameterValue
}
####
item text --> $value.split('#')[0]
vlaue --> $value.split('#')[1]
avatar
Support
Jan 12, 2022
Hello Robert,

Thank you for clarifying. For us to provide you with the updated script, please, specify what should be done if a value taken from the property pattern does not have the hash character in it.
avatar
Robert Aich
Jan 13, 2022
Hello,
Thank you :)

In my case it will be enough to skip the entry where the hash character is missing and place a log- message about it.
avatar
Support
Jan 14, 2022
Hello Robert,

here is the updated script. In the script we added theĀ $separatorĀ variable that specifies the character used to determine parameter item value and display text.

Edit Remove
PowerShell
$propertyName = "Department" # TODO: modify me
$separator = "#" # TODO: modify me
$propertyPatternDN = "CN=User Pattern,CN=Builtin,CN=Property Patterns,CN=Configuration Objects,CN=Adaxes Configuration,CN=Adaxes" # TODO: modify me
$customCommandDN = "CN=Change Department,CN=Custom Commands,CN=Configuration Objects,CN=Adaxes Configuration,CN=Adaxes" # TODO: modify me
$parameterName = "param-departmentName" # TOOD: modify me

# Bind to the Property Pattern
$propertyPattern = $Context.BindToObjectByDN($propertyPatternDN)

# Get allowed property values
$values = New-Object System.Collections.ArrayList
foreach ($item in $propertyPattern.Items)
{
    if ($item.PropertyName -ne $propertyName)
    {
        continue
    }
    
    $constraints =  $item.GetConstraints()
    try
    {
        $constraint = $constraints.GetConstraint("ADM_PROPERTYCONSTRAINTCATEGORY_VALUEFORMAT")
    }
    catch
    {
        $Context.LogMessage("No allowed values are specified for the '$propertyName' property in the property pattern.", "Warning")
        return
    }
    
    
    if ($constraint.Type -eq "ADM_PROPERTYCONSTRAINTTYPE_VALUERANGE")
    {
        # Get allowed values
        $constraint.Values | %%{[void]$values.Add($_)}
    }
    else
    {
        $Context.LogMessage("No allowed values are specified for the '$propertyName' property in the property pattern.", "Warning")
        return
    }
    
}

if ($values.Count -eq 0)
{
    $Context.LogMessage("No constraint is defined for the '$propertyName' property in the property pattern", "Warning")
    return # Property is not defined in the Property Pattern
}

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

# Update the list of allowed values
$parameterValues = @()
foreach ($value in $values)
{
    $valueParts = $value.Split("#")
    if ($valueParts.Length -eq 1)
    {
        continue
    }
    
    $parameterValue = $parameter.CreateValue()
    $parameterValue.Value = $valueParts[0]
    $parameterValue.DisplayName = $valueParts[1]
    $parameterValues += $parameterValue
}

if ($parameterValues.Length -eq 0)
{
    $Context.LogMessage("No value with separator found.", "Error")
    return
}

$parameter.Values = $parameterValues
$customCommand.Parameters = $parameters
$customCommand.SetInfo()
avatar
Robert Aich
Jan 17, 2022
Thank you,
for providing adjusted script.
works like a charm.
Leave a comment