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

Copy allowed property values from property pattern to custom command parameter

April 01, 2024 Views: 1468

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 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" # TODO: 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 13
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.
avatar
DRiVSSi Aug 30, 2023
Hello,
I have a custom command with an edit box, text input only, the text input is then loaded via script into a pdf with %param-text%. However, when I enter ' or ^etc. in the text input, it gives an error because it is considered as code. How can this be prevented?
Thx
avatar
Support Aug 30, 2023
Hello,

Most probably, you are facing errors because the value reference used in the script is not enclosed in double quotes (" character). For details, have a look at the Value references in scripts section of the following help article: https://www.adaxes.com/help/ValueReferences/#value-references-in-scripts. Alternatively, you can get the parameter value using the GetParameterValue method of the ExecuteScriptContext class. For example, the code should look like this:

Edit Remove
PowerShell
$myVariable = $Context.GetParameterValue("param-text")
avatar
AVGuys Mar 29, 2024
I am trying to use this script to import a list of locations I have defined in Property Patterns > User > Office to a Custom Command I have with a Drop Down parameter.

I have Office as a property filled in Property Patterns, with the constraint "Must be one of: Site A, Site B, Site C"

Now when I run the script, I get the error:
No constraint is defined for the 'Office' Property in the property pattern.
Which is not true, it is defined, and I made sure I copied the correct DN of the User property pattern and the destination custom command.
avatar
Support Apr 01, 2024
Hello,

It looks like you set the $propertyName to Office which is incorrect. There is no property with LDAP name Office (it is just a display name and will not work). If that is correct, try setting the variable to physicalDeliveryOfficeName (that is the LDAP name of the Office property).
avatar
AVGuys Apr 01, 2024
Oh my.... how silly of me! I should've known this, but wrongly assumed it was reading whatever was actually displayed in the Property column instead of the actual LDAP property name. Thank you, this worked like a charm!
Leave a comment
Loading...

Got questions?

Support Questions & Answers