Script repository

Check if approval request exists

Updated on: Jan 18, 2026, Views: 4895

Approval requests

The scripts return true if there is at least one pending approval request for the target object created by the same initiator. The 1st script checks for the existence of any pending request while the 2nd script checks for requests to execute a specific custom command. Both scripts can be used in the If PowerShell script returns true condition in business rules, custom commands, and scheduled tasks.

Script 1: Check for any requests

# Get GUIDs of initiator and target object.
$targetObjectGuid = $Context.TargetObject.Get("objectGuid")
$initiatorGuid = $Context.Initiator.UserAdsObject.Get("objectGuid")

# Build search criteria.
$criteria = New-AdmCriteria -Type "adm-ApprovalRequest" `
   -Expression {adm-ApprovalState -eq 0 -and adm-TargetObjectGuid -eq $targetObjectGuid -and adm-ApprovalRequestorGuid -eq $initiatorGuid}

# Search parameters
$approvalsPath = $Context.GetWellKnownContainerPath("ApprovalRequests")
$searcher = $Context.BindToObject($approvalsPath)
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
$searcher.Criteria = $criteria
$searcher.SizeLimit = 1

try
{
    # Execute search
    $searchResultIterator = $searcher.ExecuteSearch()
    $searchResults = $searchResultIterator.FetchAll()
    
    $Context.ConditionIsMet = $searchResults.Length -gt 0
}
finally
{
    # Release resources.
    if ($searchResultIterator){ $searchResultIterator.Dispose() }
}

Script 2: Check for requests to execute a custom command

In the script, the $commandID variable specifies the identifier of the custom command to search the approval requests for. For information on how to get the identifier, see Get custom command identifier.

$commandID = "{ac209b5d-042f-4165-b9f6-f73393fcba1c}" # TODO: modify me

# Get GUIDs of initiator and target object.
$targetObjectGuid = $Context.TargetObject.Get("objectGuid")
$initiatorGuid = $Context.Initiator.UserAdsObject.Get("objectGuid")

# Build search criteria.
$criteria = New-AdmCriteria -Type "adm-ApprovalRequest" `
   -Expression {adm-ApprovalState -eq 0 -and adm-TargetObjectGuid -eq $targetObjectGuid -and adm-ApprovalRequestorGuid -eq $initiatorGuid}

# Search parameters
$approvalsPath = $Context.GetWellKnownContainerPath("ApprovalRequests")
$searcher = $Context.BindToObject($approvalsPath)
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.Criteria = $criteria

try
{
    # Execute search
    $searchResultIterator = $searcher.ExecuteSearch()
    $searchResults = $searchResultIterator.FetchAll()
}
finally
{
    # Release resources.
    if ($searchResultIterator){ $searchResultIterator.Dispose() }
}

# Check each approval request if search results are not empty.
$Context.ConditionIsMet = $false
foreach ($result in $searchResults)
{
    # Bind to the approval request.
    $request = $Context.BindToObjectBySearchResult($result)
    
    if ($request.ActionToApprove.CustomCommandId -eq $commandID)
    {
        $Context.ConditionIsMet = $true
        return
    }
}

Comments 0

You must be signed in to comment.

    Got questions?

    Support Questions & Answers

    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.