Script repository
The script allows you to find and replace text in scripts executed by 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 all scripts.
.MyScript.ps1 -textToSearch "Fabrikam" -replaceWith "Acme"This command lists all business rules, custom commands and scheduled tasks where text Unmanaged Accounts is used in scripts.
.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";
}
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.GetOperationNameByType($objectType, "ADM_ACTIONNAMEFORMAT_NONE")
}
[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]
$criteria = New-Object "Softerra.Adaxes.Directory.Criteria.Criteria"
[void]$criteria.AddType($type)
$configurationContainer.Criteria = $criteria
$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.DisplayNameBuilder]::GetConfigObjectName($objectID.AdsPath.DN, $NULL)
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."
}
Comments 0
You must be signed in to comment.