We use cookies to improve your experience.
By your continued use of this site you accept such use.
For more details please see our privacy policy and cookies policy.

Script repository

All business rules, custom commands and scheduled tasks executing a specific custom command

November 20, 2024 Views: 712

The script creates a file containing all business rules, custom commands and scheduled tasks executing the specified custom command. The script can be executed in a custom command or scheduled task.

Parameters:

  • $customCommandID - Specifies the identifier of the custom command to search for. For details on how to get the identifier, see /sdk/HowDoI.GetConfigObjectID/.
  • $csvFilePath - Specifies the path to the file that will contain search results.
Edit Remove
PowerShell
$customCommandID = "{9DB88EC3-1241-4AB1-9612-C7C982BAA49F}"
$csvFilePath = "C:\Scripts\Report.csv" # TODO: modify me

function IsActionFound ($actions, $customCommandID)
{
    foreach ($action in $actions)
    {
        if ($action.Class -ne "adm-CustomCommandAction")
        {
            continue
        }
        
        $customCommandAction = $action.GetAction()
        if ($customCommandAction.CustomCommandId -ne $customCommandID)
        {
            continue
        }
        
        return $True
    }
    
    return $False
}

function IsContainsExecuteCommandAction ($configurationObject, $customCommandID)
{
    for ($i = 0; $i -lt $configurationObject.ConditionedActions.Count; $i++)
    {
        $conditionedActions = $configurationObject.ConditionedActions.GetObject($i)
        
        if (IsActionFound $conditionedActions.Actions $customCommandID)
        {
            return $True
        }
        elseif (IsActionFound $conditionedActions.ElseActions $customCommandID)
        {
            return $True
        }
        else
        {
            for ($j = 0; $j -lt $conditionedActions.ElseIfConditionedActions.Count; $j++)
            {
                $elseIfConditionedActions = $conditionedActions.ElseIfConditionedActions.GetObject($j)
                if (IsActionFound $elseIfConditionedActions.Actions $customCommandID)
                {
                    return $True
                }
            }
        }
    }
    
    return $False
}

$configurationContainersToObjectTypes = @{
    "CustomCommands" = "adm-CustomCommand";
    "BusinessRules" = "adm-BusinessRule";
    "ScheduledTasks" = "adm-ScheduledTask";
}

$records = New-Object System.Collections.ArrayList
foreach ($item in $configurationContainersToObjectTypes.GetEnumerator())
{
    # Search configuration objects
    $path = $Context.GetWellKnownContainerPath($item.Key)
    $searcher = $Context.BindToObject($path)
    $criteria = New-AdmCriteria $item.Value
    $searcher.Criteria = $criteria
    $searcher.SearchScope = "ADS_SCOPE_SUBTREE"
    $searcher.PageSize = 500

    try
    {
        # Execute search
        $searchResultIterator = $searcher.ExecuteSearch()
        $searchResults = $searchResultIterator.FetchAll()

        # Search 'Execute Custom Command' action
        foreach ($searchResult in $searchResults)
        {
            $configurationObject = $Context.BindToObjectBySearchResult($searchResult)
            if (IsContainsExecuteCommandAction $configurationObject $customCommandID)
            {
                $record = New-Object PSObject
                $record | Add-Member -MemberType NoteProperty -Name Name -Value $configurationObject.Get("name")
                $record | Add-Member -MemberType NoteProperty -Name Path -Value $configurationObject.AdsPath
                $records.Add($record)
            }
        }
    }
    finally
    {
        # Release resources
        $searchResultIterator.Dispose()
    }
}

$records | Export-Csv -Path $csvFilePath -NoTypeInformation
Comments 2
avatar
Olegs Nov 20, 2024
Hi!
Seems there is mistake in loop
for ($j = 0; $j -lt $conditionedActions.ElseIfConditionedActions.Count; $j++)
{
$elseIfConditionedActions = $conditionedActions.ElseIfConditionedActions.GetObject($i)
if (IsActionFound $elseIfConditionedActions.Actions $customCommandID)
You should use $J instead of $i in "For $elseIfConditionedActions = $conditionedActions.ElseIfConditionedActions.GetObject($i)"
avatar
Support Nov 20, 2024
Hello Olegs,

Thank you for pointing out the issue. We updated the script accordingly.
Leave a comment
Loading...

Got questions?

Support Questions & Answers