Script Repository


Add approvers to Approval Requests

September 17, 2015
1720

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 )
No results found.
Leave a comment