Script repository
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. The script should be executed in Windows PowerShell on the computer where Adaxes service runs.
To use the script, install the Adaxes PowerShell module on the computer where the service runs.
Sample usage
This command replaces the word Fabrikam with the word Acme in email notifications.
.\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.
.\MyScript.ps1 -textToSearch "Unmanaged Accounts"The script
param(
[Parameter(Mandatory=$true,Position=1)]
[String]$textToSearch,
[Parameter(Position=2)]
[String]$replaceWith
)
Import-Module Adaxes
# 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.GetOperationNameByType($objectType, "ADM_ACTIONNAMEFORMAT_NONE")
}
[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]
$criteria = New-Object "Softerra.Adaxes.Directory.Criteria.Criteria"
[void]$criteria.AddType($type)
$configurationContainer.Criteria = $criteria
$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.DisplayNameBuilder]::GetConfigObjectName($searchResult.AdsPath.DN, $NULL)
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
You must be signed in to comment.