Script Repository


Search and replace text in Adaxes scripts

March 02, 2020
1316

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:

Edit Remove
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:

Edit Remove
PowerShell
.\MyScript.ps1 -textToSearch "Unmanaged Accounts"

The script:

Edit Remove
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 CheckActionAndConditionSets($actionsAndConditionsSets, $textToSearch, $replaceWith, $objectType, $actionConditionList)
{
    $scriptActionCount = 0
    $scriptConditionCount = 0
    foreach ($actionsAndConditionsSet in $actionsAndConditionsSets)
    {
        # Check actions
        foreach ($action in $actionsAndConditionsSet.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")

            $scriptActionCount++

            if ([System.String]::IsNullOrEmpty($replaceWith))
            {
                continue
            }
            
            # Update the action
            $script = $script.Replace($textToSearch, $replaceWith)
            $objectAction.Script = $script
            $action.SetAction($objectAction)
            $action.SetInfo()
        }
        
        # 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")

            $scriptConditionCount++

            if ([System.String]::IsNullOrEmpty($replaceWith))
            {
                continue
            }
            
            # Update Condition
            $script = $script.Replace($textToSearch, $replaceWith)
            $objectCondition.Script = $script
            $condition.SetCondition($objectCondition)
            $condition.SetInfo()
        }
    }
    return $scriptActionCount, $scriptConditionCount
}

# 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
        $result = CheckActionAndConditionSets $object.ConditionedActions $textToSearch $replaceWith $object.ObjectType $actionConditionList
        
        if (($result[0] -eq 0) -and ($result[1] -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 ($result[0] -eq 0)
        {
            $totalConditions = $totalConditions + $result[1]
            Write-Host "Search text found in" $result[1] "condition(s)"
            Write-Host
        }
        elseif ($result[1] -eq 0)
        {
            $totalActions = $totalActions + $result[0]
            Write-Host "Search text found in" $result[0] "action(s)"
            Write-Host
        }
        else
        {
            $totalActions = $totalActions + $result[0]
            Write-Host "Search text found in" $result[0] "action(s)"
            
            $totalConditions = $totalConditions + $result[1]
            Write-Host "Search text found in" $result[1] "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."
}


Comments ( 0 )
No results found.
Leave a comment