I'm not sure how to update the script. Thanks for your help!
The following script is in a custom command which will be executed by a scheduled task every 30 min
$patternName = "Benutzer-Muster" # TODO: modify me
$propertyToUpdate = "extensionAttribute5" # 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)