Script Repository


Search and replace text in Adaxes email notifications

August 31, 2020
39

The script finds and replaces the specified text in the notifications sent by the Send email notification action in Business Rules, Custom Commands and Scheduled Tasks.

Parameters:

  • $textToSearch - Specifies the text to search for.
  • $replaceWith (optional) - Specifies the optonal 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 email notifications:

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 email notifications:

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";
} # TODO: comment unnecessary

function CheckActions($actions, $textToSearch, $replaceWith, $objectType, $actionList, $actionCount)
{
    foreach ($action in $actions)
    {
        if ($action.Class -ne "adm-SendMailNotificationAction")
        {
            continue
        }
            
        # Check whether the action contains the specified text
        $objectAction = $action.GetAction()
        $mailNotification = $objectAction.Text
        if (!($mailNotification.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")
        }
        [void]$actionList.AppendLine("`t $actionDescription")

        $actionCount++

        if ([System.String]::IsNullOrEmpty($replaceWith))
        {
            continue
        }
            
        # Update the action
        $mailNotification = $mailNotification.Replace($textToSearch, $replaceWith)
        $objectAction.Text = $mailNotification
        $action.SetAction($objectAction)
        $action.SetInfo()
    }
    return $actionCount
}

# Connect to the Adaxes service
$admNS = New-Object "Softerra.Adaxes.Adsi.AdmNamespace"
$admService = $admNS.GetServiceDirectly("localhost")

$totalActions = 0

foreach ($alias in $configurationObjectInfos.Keys)
{
    try
    {
        # 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
        $type = $configurationObjectInfos[$alias]
        $configurationContainer.SearchFilter =  "(objectCategory=$type)"
        $configurationContainer.PageSize = 500
        $configurationContainer.SearchScope = "ADS_SCOPE_SUBTREE"
    
        $searchResultIterator = $configurationContainer.ExecuteSearch()
        $searchResults = $searchResultIterator.FetchAll()

        # Search the specified text in email notifications
        foreach ($searchResult in $searchResults)
        {
            # Bind to the Business Rule, Custom Command or Scheduled Task
            $object = $admService.OpenObject($searchResult.AdsPath, $NULL, $NULL, 0)
            $actionList = New-Object "System.Text.StringBuilder"
        
            $actionCount = 0
            foreach ($conditionedActions in $object.ConditionedActions)
            {
                # Check actions
                $actionCount = CheckActions $conditionedActions.Actions $textToSearch $replaceWith $object.ObjectType $actionList $actionCount

                # Check ElseIf blocks
                foreach ($elseIfConditionedActions in $conditionedActions.ElseIfConditionedActions)
                {
                    $actionCount = CheckActions $elseIfConditionedActions.Actions $textToSearch $replaceWith $object.ObjectType $actionList $actionCount
                }
 
                # Check Else block
                $actionCount = CheckActions $conditionedActions.ElseActions $textToSearch $replaceWith $object.ObjectType $actionList $actionCount
            }
                
            if ($actionCount -eq 0)
            {
                continue
            }
        
            # Output actions found
            $objectDisplayName = [Softerra.Adaxes.Utils.ObjectNameHelper]::GetObjectName($searchResult.AdsPath, "IncludeParentPath")
            Write-Host "Object name: $objectDisplayName" 
        
            Write-Host "Action descriptions:"
            Write-Host $actionList.ToString()

            $totalActions = $totalActions + $actionCount
            Write-Host "Search text found in" $actionCount "action(s)"
            Write-Host
        }
    }
    finally
    {
        if ($searchResultIterator) { $searchResultIterator.Dispose() }
    }
}

if ([System.String]::IsNullOrEmpty($replaceWith))
{
    Write-Host "Total actions found:" $totalActions
}
else
{
    Write-Host "Search text replaced in $totalActions actions."
}


Comments ( 0 )
No results found.
Leave a comment