The script allows you to find and replace text in scripts executed by business rules, custom commands and scheduled tasks.
Parameters:
- $textToSearch - Specifies the text to search for.
- $replaceWith (optional) - Specifies the text to replace all occurrences of $textToSearch with. If you omit this parameter, the script only lists all business rules, custom commands and scheduled tasks where the search text occurs.
Sample Usage:
This sample replaces the word Fabrikam with the word Acme in all scripts:
PowerShell
.\MyScript.ps1 -textToSearch "Fabrikam" -replaceWith "Acme"
The following example lists all business rules, custom commands and scheduled tasks where text Unmanaged Accounts is used in scripts:
PowerShell
.\MyScript.ps1 -textToSearch "Unmanaged Accounts"
The script:
PowerShell
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 = @{
"BusinessRules" = "adm-BusinessRule";
"CustomCommands" = "adm-CustomCommand";
"ScheduledTasks" = "adm-ScheduledTask";
}
function CheakActions ($actions, $textToSearch, $replaceWith, $objectType, $actionConditionList, $actionConditionCount)
{
foreach ($action in $actions)
{
if ($action.Class -ne "adm-RunScriptAction")
{
continue
}
# Check whether the script contains the specified text
$objectAction = $action.GetAction()
$script = $objectAction.Script
if (!($script.ToLower().Contains($textToSearch.ToLower())))
{
continue
}
# Get action description
$actionDescription = $objectAction.CustomDescription
if ([System.String]::IsNullOrEmpty($actionDescription))
{
$actionDescription = $objectAction.GetOperationName("The $objectType", $NULL,
"ADM_ACTIONNAMEFORMAT_SPECIFIC,
ADM_ACTIONNAMEFORMAT_CAPITALLETTER,
ADM_ACTIONNAMEFORMAT_PRESENTTENSE,
ADM_ACTIONNAMEFORMAT_SPECIFICINFOINADDITIONALDATA")
}
[void]$actionConditionList.AppendLine("`tAction: $actionDescription")
$actionConditionCount.ActionCount++
if ([System.String]::IsNullOrEmpty($replaceWith))
{
continue
}
# Update the action
$script = $script.Replace($textToSearch, $replaceWith)
$objectAction.Script = $script
$action.SetAction($objectAction)
$action.SetInfo()
}
}
function CheckActionAndConditionSets($actionsAndConditionsSets, $textToSearch, $replaceWith, $objectType, $actionConditionList, $actionConditionCount, $isElseIfBlock)
{
foreach ($actionsAndConditionsSet in $actionsAndConditionsSets)
{
# Check actions
CheakActions $actionsAndConditionsSet.Actions $textToSearch $replaceWith $objectType $actionConditionList $actionConditionCount
# Check conditions
foreach ($condition in $actionsAndConditionsSet.Conditions)
{
if ($condition.Class -ne "adm-ScriptCondition")
{
continue
}
# Check whether the script contains the specified string
$objectCondition = $condition.GetCondition()
$script = $objectCondition.Script
if (!($script.ToLower().Contains($textToSearch.ToLower())))
{
continue
}
# Get condition description
$conditionDescription = $objectCondition.GetDescription($NULL)
[void]$actionConditionList.AppendLine("`tCondition: $conditionDescription")
$actionConditionCount.ConditionCount++
if ([System.String]::IsNullOrEmpty($replaceWith))
{
continue
}
# Update Condition
$script = $script.Replace($textToSearch, $replaceWith)
$objectCondition.Script = $script
$condition.SetCondition($objectCondition)
$condition.SetInfo()
}
if ($isElseIfBlock)
{
continue
}
CheckActionAndConditionSets $actionsAndConditionsSet.ElseIfConditionedActions $textToSearch $replaceWith $objectType $actionConditionList $actionConditionCount $True
if ($actionsAndConditionsSet.ElseActions.Count -ne 0)
{
CheakActions $actionsAndConditionsSet.ElseActions $textToSearch $replaceWith $objectType $actionConditionList $actionConditionCount
}
}
}
# Connect to Adaxes Service
$admNS = New-Object "Softerra.Adaxes.Adsi.AdmNamespace"
$admService = $admNS.GetServiceDirectly("localhost")
$totalActions = 0
$totalConditions = 0
foreach ($alias in $configurationObjectInfos.Keys)
{
# Bind to the configuration container that contains 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 scripts for the specified string
foreach ($objectID in $objects)
{
# Bind to the Business Rule, Custom Command or Scheduled Task
$object = $admService.OpenObject($objectID.AdsPath, $NULL, $NULL, 0)
$actionConditionList = New-Object "System.Text.StringBuilder"
# Perform search in scripts
$actionConditionCount = @{ ActionCount = 0; ConditionCount = 0 }
CheckActionAndConditionSets $object.ConditionedActions $textToSearch $replaceWith $object.ObjectType $actionConditionList $actionConditionCount $False
if (($actionConditionCount.ActionCount -eq 0) -and ($actionConditionCount.ConditionCount -eq 0))
{
continue
}
# Output scripts found
$objectDisplayName = [Softerra.Adaxes.Utils.ObjectNameHelper]::GetObjectName($objectID.AdsPath, "IncludeParentPath")
Write-Host "Object name: $objectDisplayName"
Write-Host "Action/Condition description:"
Write-Host $actionConditionList.ToString()
if ($actionConditionCount.ActionCount -eq 0)
{
$totalConditions = $totalConditions + $actionConditionCount.ConditionCount
Write-Host "Search text found in" $actionConditionCount.ConditionCount "condition(s)"
Write-Host
}
elseif ($actionConditionCount.ConditionCount -eq 0)
{
$totalActions = $totalActions + $actionConditionCount.ActionCount
Write-Host "Search text found in" $actionConditionCount.ActionCount "action(s)"
Write-Host
}
else
{
$totalActions = $totalActions + $actionConditionCount.ActionCount
Write-Host "Search text found in" $actionConditionCount.ActionCount "action(s)"
$totalConditions = $totalConditions + $actionConditionCount.ConditionCount
Write-Host "Search text found in" $actionConditionCount.ConditionCount "condition(s)"
Write-Host
}
}
}
if ([System.String]::IsNullOrEmpty($replaceWith))
{
Write-Host "Total actions found:" $totalActions
Write-Host "Total conditions found:" $totalConditions
}
else
{
Write-Host "Search text replaced in $totalActions actions."
Write-Host "Search text replaced in $totalConditions conditions."
}