Script Repository


Update Property Pattern item with names of groups located in a container

September 15, 2020
267

Te script updates values allowed for a property by a Property Pattern with the names of groups located in a container. To run the script, create a Custom Command, Business Rule or Scheduled task configured for the Organizational Unit or Container object type.

Parameters

  • $patternName - Specifies the name of the Property Pattern to update.
  • $propertyToUpdate - Specifies the LDAP anme of the property for which the Property pattern item will be updated.
Edit Remove
PowerShell
$patternName = "User Pattern" # TODO: modify me
$propertyToUpdate = "department" # TODO: modify me

function SearchObjects($filter, $containerPath)
{
    $searcher = $Context.BindToObject($containerPath)
    $searcher.SearchFilter = $filter
    $searcher.SearchScope = "ADS_SCOPE_SUBTREE"
    $searcher.PageSize = 500
    $searcher.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"     
    
    try
    {
        $searchResultIterator = $searcher.ExecuteSearch()
        $searchResults = $searchResultIterator.FetchAll()
        
        return ,$searchResults
    }
    finally
    {
        # Release resources
        if ($searchResultIterator){ $searchResultIterator.Dispose() }
    }
}

# Get all groups in the target OU
$groupSearchResults = SearchObjects "(objectCategory=group)" $Context.TargetObject.AdsPath

# Exit if no groups found
if ($groupSearchResults.Length -eq 0)
{
    return
}

# Get group names
$groupNames = New-Object System.Collections.ArrayList
foreach ($searchResult in $groupSearchResults)
{
    $groupNames.Add($searchResult.Properties["name"].Value)
}

# Find Property Pattern
$propertyPatternsPath = $Context.GetWellKnownContainerPath("PropertyPatterns")
$patternSearchResults = SearchObjects "(&(objectCategory=adm-PropertyPattern)(name=$patternName))" $propertyPatternsPath

if ($patternSearchResults.Length -gt 1)
{
    $Context.LogMessage("Found more than one Property Pattern with name '$patternName'.", "Warning")
    return
}
if ($patternSearchResults.Length -eq 0)
{
    $Context.LogMessage("Property Pattern '$patternName' does not exist.", "Error")
    return
}

# Bind to the Property Pattern
$pattern = $Context.BindToObject($patternSearchResults[0].AdsPath)

# Delete the pattern item for the property
foreach ($item in $pattern.Items)
{
    if ($item.PropertyName -ieq $propertyToUpdate)
    {
        $pattern.Items.Remove($item)
        break
    }
}

# Create list of values for the property
$item = $pattern.Items.Create()
$item.PropertyName = $propertyToUpdate

$constraints = $item.GetConstraints()
$constraint = $constraints.Create(
    "ADM_PROPERTYCONSTRAINTTYPE_VALUERANGE")
$constraint.AreValuesDenied = $False
$constraint.Values = $groupNames.ToArray()
$constraints.Add($constraint)
$item.SetConstraints($constraints)

# Update Property Pattern
$item.SetInfo()
$pattern.Items.Add($item)


Comments ( 0 )
No results found.
Leave a comment