Script Repository


Check if approval request exists

February 25, 2021
493

The script returns false if there is a pending approval request for the target object created by the same initiator. The script can be used in the If PowerShell script returns true condition in business rules, custom commands and scheduled tasks.

In the script, the $days variable specifies the number of days during which the existing approval request should be created to meet the condition.

Edit Remove
PowerShell
$days = 25 # TODO: modify me

# Build filter
$filter = New-Object "System.Text.StringBuilder"
$filter.Append("(&(objectClass=adm-ApprovalRequest)(adm-ApprovalState=0)")

$startTime = ((Get-Date).AddDays(-$days)).ToFileTime()
$filter.Append("(adm-ApprovalRequestCreationTime>=$startTime)")

# Add target object to filter
$targetObjectGuid = New-Object "System.Guid" (, $Context.TargetObject.Get("objectGUID"))
$targetObjectGuidFilter = [Softerra.Adaxes.Ldap.FilterBuilder]::Create("adm-TargetObjectGuid", $targetObjectGuid)
$filter.Append($targetObjectGuidFilter)

# Add initiator to filter
$initiatorGuid = New-Object "System.Guid" (, $Context.Initiator.UserAdsObject.Get("objectGuid"))
$initiatorGuidFilter = [Softerra.Adaxes.Ldap.FilterBuilder]::Create("adm-ApprovalRequestorGuid", $initiatorGuid)
$filter.Append($initiatorGuidFilter)

# Finish building filter
$filter.Append(")")

# Search approval requests
$approvalsPath = $Context.GetWellKnownContainerPath("ApprovalRequests")

$searcher = $Context.BindToObject($approvalsPath)
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.SearchFilter = $filter.ToString()
$searcher.SizeLimit = 1
    
try
{
    $searchResultIterator = $searcher.ExecuteSearch()
    $searchResults = $searchResultIterator.FetchAll()
    
    $Context.ConditionIsMet = $searchResults.Length -eq 0
}
finally
{
    # Release resources
    if ($searchResultIterator){ $searchResultIterator.Dispose() }
}

The below script checks existing approval requests independently of their creation date.

Edit Remove
PowerShell
# Build filter
$filter = New-Object "System.Text.StringBuilder"
$filter.Append("(&(objectClass=adm-ApprovalRequest)(adm-ApprovalState=0)")

# Add target object to filter
$targetObjectGuid = New-Object "System.Guid" (, $Context.TargetObject.Get("objectGUID"))
$targetObjectGuidFilter = [Softerra.Adaxes.Ldap.FilterBuilder]::Create("adm-TargetObjectGuid", $targetObjectGuid)
$filter.Append($targetObjectGuidFilter)

# Add initiator to filter
$initiatorGuid = New-Object "System.Guid" (, $Context.Initiator.UserAdsObject.Get("objectGuid"))
$initiatorGuidFilter = [Softerra.Adaxes.Ldap.FilterBuilder]::Create("adm-ApprovalRequestorGuid", $initiatorGuid)
$filter.Append($initiatorGuidFilter)

# Finish building filter
$filter.Append(")")

# Search approval requests
$approvalsPath = $Context.GetWellKnownContainerPath("ApprovalRequests")

$searcher = $Context.BindToObject($approvalsPath)
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.SearchFilter = $filter.ToString()
$searcher.SizeLimit = 1
    
try
{
    $searchResultIterator = $searcher.ExecuteSearch()
    $searchResults = $searchResultIterator.FetchAll()
    
    $Context.ConditionIsMet = $searchResults.Length -eq 0
}
finally
{
    # Release resources
    if ($searchResultIterator){ $searchResultIterator.Dispose() }
}

Comments ( 0 )
No results found.
Leave a comment