Hello,
We have updated the script to meet your needs, Find it below.
# Search settings
$propertyForList = "distinguishedName" # TOOD: modify me
$propertyForSearch = "description" # TOOD: modify me
$valuesForSearch = @("Value1", "Value2") # TOOD: modify me
$sortBy = "name" # TODO: modify me
$sortDirection = "Ascending" # TODO: modify me. Possible values: Ascending, Descending
# Property Pattern settings
$patternName = "User Pattern" # TODO: modify me
$propertyToUpdate = "manager" # TODO: modify me
$isPropertyRequired = $True # TODO: modify me
function SearchObjects($filter, $properties, $baseObjectPath, $virtualRoot, $sortBy, $sortDirection)
{
$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
if ($sortBy -ne $NULL)
{
$sortOption = New-Object "Softerra.Adaxes.Adsi.AdmSortOption"
$sortOption.PropertyName = $sortBy
$sortOption.Direction = $sortDirection
$searcher.Sort = $sortOption
}
try
{
$searchResultIterator = $searcher.ExecuteSearch()
$searchResults = $searchResultIterator.FetchAll()
return ,$searchResults
}
finally
{
# Release resources
if ($searchResultIterator){ $searchResultIterator.Dispose() }
}
}
# Search Property Pattern
$propertyPatternsPath = $Context.GetWellKnownContainerPath("PropertyPatterns")
$searchResults = SearchObjects "(&(objectClass=adm-PropertyPattern)(name=$patternName))" @() $propertyPatternsPath $False $NULL $NULL
if ($searchResults.Length -eq 0)
{
$Context.LogMessage("Property Pattern '$patternName' not found.", "Warning")
return
}
elseif ($searchResults.Length -gt 1)
{
$Context.LogMessage("Found more than one Property Pattern with the following name: '$patternName'.", "Warning")
return
}
$propertyPatternPath = $searchResults[0].AdsPath
# Build search filter for users
$filter = New-Object "System.Text.StringBuilder"
$filter.Append("(&(sAMAccountType=805306368)($propertyForList=*)")
$filter.Append([Softerra.Adaxes.Ldap.FilterBuilder]::CreateOrred($propertyForSearch, [System.String[]]$valuesForSearch))
$filter.Append(")")
# Search users
$searchResults = SearchObjects $filter.ToString() @($propertyForList) "Adaxes://RootDSE" $True $sortBy $sortDirection
$values = New-Object "System.Collections.ArrayList"
foreach ($searchResult in $searchResults)
{
[void]$values.Add($searchResult.Properties[$propertyForList].Value)
}
# Update Property Pattern
$pattern = $Context.BindToObject($propertyPatternPath)
foreach ($item in $pattern.Items)
{
if ($item.PropertyName -ieq $propertyToUpdate)
{
$pattern.Items.Remove($item)
break
}
}
if ($values.Count -eq 0)
{
return
}
# Create a new item
$item = $pattern.Items.Create()
$item.PropertyName = $propertyToUpdate
$item.IsPropertyRequired = $isPropertyRequired
$constraints = $item.GetConstraints()
$constraint = $constraints.Create("ADM_PROPERTYCONSTRAINTTYPE_VALUERANGE")
$constraint.AreValuesDenied = $False
$constraint.Values = $values.ToArray()
$constraints.Add($constraint)
$item.SetConstraints($constraints)
# Save the changes
$item.SetInfo()
$pattern.Items.Add($item)