Hello,
Thank you for the provided details. The thing is that the script searches for the exact text to replace and value references (e.g. %adm-CustomAttributeText1%) can only be used in the value part of the condition. We updated the script according to your requirements, find it below.
param(
[Parameter(Mandatory=$true,Position=1)]
[String]$textToSearch,
[Parameter(Position=2)]
[String]$replaceWith
)
[void][Reflection.Assembly]::LoadWithPartialName("Softerra.Adaxes.Adsi")
# A hash table with types of configuration objects and their aliases
$configurationObjectInfos = @{
"CustomCommands" = "adm-CustomCommand";
"BusinessRules" = "adm-BusinessRule"
}
function CheckConditions($conditions, $textToSearch, $replaceWith, $objectType, $actionConditionList)
{
$scriptConditionCount = 0
foreach ($condition in $conditions)
{
if ($condition.Class -ne "adm-AttributeOperatorValueCondition")
{
continue
}
# Check whether AttributeName contains the specified string
$objectCondition = $condition.GetCondition()
$attributeName = $objectCondition.AttributeName
if($null -eq $attributeName)
{
continue
}
if (!($attributeName.ToLower() -eq $textToSearch.ToLower()))
{
continue
}
# Get condition description
$conditionDescription = $objectCondition.GetDescription($NULL)
[void]$actionConditionList.AppendLine("`tCondition: $conditionDescription")
$scriptConditionCount++
if ([System.String]::IsNullOrEmpty($replaceWith))
{
continue
}
# Update condition
$attributeName = $replaceWith
$objectCondition.AttributeName = $attributeName
$condition.SetCondition($objectCondition)
$condition.SetInfo()
}
return ,$scriptConditionCount
}
function CheckActionAndConditionSets($actionsAndConditionsSets, $textToSearch, $replaceWith, $objectType, $actionConditionList)
{
$scriptConditionCount = 0
foreach ($actionsAndConditionsSet in $actionsAndConditionsSets)
{
# Check conditions
$result = CheckConditions $actionsAndConditionsSet.Conditions $textToSearch $replaceWith $objectType $actionConditionList
$scriptConditionCount += $result[0]
foreach ($elseIf in $actionsAndConditionsSet.ElseIfConditionedActions)
{
$result = CheckConditions $elseIf.Conditions $textToSearch $replaceWith $objectType $actionConditionList
$scriptConditionCount += $result[0]
}
}
return ,$scriptConditionCount
}
# Connect to the Adaxes Service
$admNS = New-Object "Softerra.Adaxes.Adsi.AdmNamespace"
$admService = $admNS.GetServiceDirectly("localhost")
$totalConditions = 0
foreach ($alias in $configurationObjectInfos.Keys)
{
# Bind to the configuration container for objects of the current type
$configurationContainerPath = $admService.Backend.GetConfigurationContainerPath($alias)
$configurationContainer = $admService.OpenObject($configurationContainerPath, $NULL, $NULL, 0)
# Find configuration objects of the current type
$type = $configurationObjectInfos[$alias]
$configurationContainer.SearchFilter = "(objectCategory=$type)"
$configurationContainer.PageSize = 500
$configurationContainer.SearchScope = "ADS_SCOPE_SUBTREE"
$configurationContainer.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
$searcherResult = $configurationContainer.ExecuteSearch()
$objects = $searcherResult.FetchAll()
$searcherResult.Dispose()
# Search conditions for the specified string
foreach ($objectID in $objects)
{
# Bind to the Business Rule or Custom Command
$object = $admService.OpenObject($objectID.AdsPath, $NULL, $NULL, 0)
$actionConditionList = New-Object "System.Text.StringBuilder"
# Perform search in conditions
$result = CheckActionAndConditionSets $object.ConditionedActions $textToSearch $replaceWith $object.ObjectType $actionConditionList
if ($result[0] -eq 0)
{
continue
}
# Output conditions found
$objectDisplayName = [Softerra.Adaxes.Utils.ObjectNameHelper]::GetObjectName($objectID.AdsPath, "IncludeParentPath")
Write-Host "Object name: $objectDisplayName"
Write-Host "Action/Condition description:"
Write-Host $actionConditionList.ToString()
$totalConditions = $totalConditions + $result[0]
Write-Host "Search text found in" $result[0] "condition(s)"
Write-Host
}
}
if ([System.String]::IsNullOrEmpty($replaceWith))
{
Write-Host "Total conditions found:" $totalConditions
}
else
{
Write-Host "Search text replaced in $totalConditions conditions."
}
To run the script, you need to use the very same command, but the parameters should contain LDAP names of the property to replace (e.g. adm-CustomAttributeText1) and the property to set (e.g. department). Finally, the command should look like the following:
.\MyScript.ps1 -textToSearch "adm-CustomAttributeText1" -replaceWith "department"