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.
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
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)"
Thank you for pointing out the issue. We updated the script accordingly.