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

Add approvers to approval requests

The script adds the user on which it is executed as a possible approver of outdated approval requests that have not been approved, denied or canceled within a predefined time frame.

To be able to process outdated approval requests, you need to create a scheduled task configured for the User object type. For more information on creating scheduled tasks, see the Schedule Tasks for Active Directory Management Tutorial. On step 10 of the Tutorial, you need to include the users whom you want to add as approvers in the Activity Scope of your task.

Parameters:

  • $requestExpirationDays - Specifies the number of days an approval request needs to remain pending to become outdated.
  • $subject - Specifies the subject of email notifications that will be sent to new approvers.
  • $messageTemplate - Specifies the email notification template. In the template, use {0} as a placeholder for the description of the operation to approve, and {1} as a placeholder for the date when the request was submnitted.
For more information on managing approval requests using Adaxes ADSI API, see Managing Approval Requests.
Edit Remove
PowerShell
$requestExpirationDays = 30 # TODO: modify me
$subject = "New request awaiting your approval" # TODO: modify me
$messageTemplate = @"
Dear %name%,

New request awaiting your approval

Operation: {0}
Requested on: {1}
"@ # TODO: modify me

# Bind to the Approval Requests container
$approvalsPath = $Context.GetWellKnownContainerPath("ApprovalRequests")
$aprovalsContainer = $Context.BindToObject($approvalsPath)

# Get all pending approval requests
$requests = $aprovalsContainer.GetApprovalRequests("ADM_APPROVALSTATE_PENDING")

$newApprover = $Context.TargetObject

foreach ($requestID in $requests)
{
    # Bind to the approval request
    $guid = [Guid]$requestID
    $request = $Context.BindToObject("Adaxes://<GUID=$guid>")

    # Check whether request has expired
    $requestExpDate = $request.CreationDate.AddDays($requestExpirationDays)
    
    if ([System.DateTime]::Now -lt $requestExpDate)
    {
        continue # Not expired yet
    }
    
    # Get information about possible approvers
    $approversInfo = $request.GetApproversInfo()
    if ($approversInfo.IsApproverEx($newApprover, $request.Requestor, $request.TargetObject))
    {
        continue # The user is already one of the possible approvers
    }
    
    # Add user as a possible approver
    $approverTrustees = $approversInfo.ApproverTrustees
    $approverTrustees.Add($newApprover)
    
    # Update the list of possible approvers
    $request.SetApproversInfo($approversInfo)
    $request.SetInfo()
    
    # Build email notification
    $operation = $request.DescriptionOfOperationToApprove
    $creationDate = $request.CreationDate
    $message = [System.String]::Format($messageTemplate, @($operation, $creationDate))
    
    # Send notification to the new approver
    $Context.SendMail("%mail%", $subject, $message, $NULL)
}

Comments 0
Leave a comment
Loading...

Got questions?

Support Questions & Answers