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

Notify users on Requests awaiting their approval

October 19, 2023 Views: 2442

The scripts can be used to notify users that there are approval requests awaiting their decision. The 1st script sends a separate notification for each approval request. The 2nd script combines all approval requests that a user can process into a single notification. Thus, for example, if a user is an approver to 10 approval request, with the 1st script, he will get 10 notification emails, 1 email for each request, and with the 2nd script he will get a single email with information on all the 10 requests.

To use the scripts in your environment, you can, for example, create a scheduled task that reminds approvers on a regular basis. The task needs to be configured for the Domain-DNS object type, and the scope should be any of your AD domains. The domain included in the scope does not affect the scope of requests included in notifications and will be used only to trigger the task.

Script 1: Separate Email for Each Approval Request

This script re-sends the approval request default approval request notifications. It has no parameters.

Edit Remove
PowerShell
# Bind to the Approval Requests container
$containerPath = $Context.GetWellKnownContainerPath("ApprovalRequests")
$container = $Context.BindToObject($containerPath)

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

foreach ($requestID in $requests)
{
    # Bind to the approval request
    $guid = New-Object "System.Guid" (,$requestID)
    $guid = $guid.ToString()
    $requestPath = "Adaxes://<GUID=$guid>"
    $request = $Context.BindToObject($requestPath)
    
    # Resend notification to approvers
    $request.NotifyApprovers()
}

Script 2: Single Notification for all Approval Requests

This version of the script sends a single notification containing all requests awaiting approval by a user.

Parameters:

  • $notificationHeader - Specifies the email notification header.
  • $notificationFooter - Specifies the email notification footer.
  • $subject - Specifies the email notification subject.
Edit Remove
PowerShell
$notificationHeader = "<b>List of unapproved operations:</b><br/><br/>" # TODO: modify me
$notificationFooter = "<hr /><p><i>Please do not reply to this e-mail, it has been sent to you for notification purposes only.</i></p>" # TODO: modify me
$subject = "Operations Awaiting Your Approval" # TODO: modify me

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

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

$requests = @{}
foreach ($requestID in $pendingRequests)
{
    # Bind to the approval request
    $requestGuid = New-Object "System.Guid" (,$requestID)
    $requestGuid = $requestGuid.ToString()
    $requestPath = "Adaxes://<GUID=$requestGuid>"
	$request = $Context.BindToObject($requestPath)
    
    # Get information on the operation awaiting approval
    $requestInfo = New-Object PSObject
    $requestInfo | add-member Noteproperty Guid $requestGuid
    $requestInfo | add-member Noteproperty Operation $request.DescriptionOfOperationToApprove
    
    # Get information on the approvers
    $approversInfo = $request.GetApproversInfo()
    $approvers = $approversInfo.GetApproversEx($request.Requestor, $request.TargetObject)
    
    foreach ($approver in $approvers)
    {
        try
        {
            $mail = $approver.Get("mail")
        }
        catch
        {
            continue
        }

        if (-not($requests.ContainsKey($mail)))
        {
            $requests.Add($mail, @($requestInfo)) | Out-Null
            continue
        }

        $requests[$mail] += $requestInfo
    }
}

# Get default Web Interface address
$webInterfaceAddress = "%adm-WebInterfaceUrl%"
if ([System.String]::IsNullOrEmpty($webInterfaceAddress))
{
    $Context.LogMessage("Default web interface address not set for the Adaxes service.", "Warning")
}

foreach ($mail in $requests.Keys)
{
    # Add all requests to e-mail
    $unapprovedTaskList = "<ol>"
    foreach ($requestInfo in $requests[$mail])
    {
        $requestGuid = $requestInfo.Guid
        $requestOperation = $requestInfo.Operation
        $unapprovedTaskList += "<li><a href='$webInterfaceAddress`#/Browse/$requestGuid'>$requestOperation</a></li>"
    }
    $unapprovedTaskList += "</ol>"
    
    # Build email notification
    $htmlBody = $notificationHeader + $unapprovedTaskList + $notificationFooter
    
    # Send notification to approver
    $Context.SendMail($mail, $subject, $NULL, $htmlBody)
}

Comments 2
avatar
Ben Cavo Aug 06, 2024
I would like not to include 'DescriptionOfOperationToApprove' on the mail, how would I add just the name of the user that was created?
Thanks
avatar
Support Aug 06, 2024
Hello Ben,

Unfortunately, there is no such possibility using the script from this article. It can only be done using a custom script the will send a custom email not the automatically generated one.
Leave a comment
Loading...

Got questions?

Support Questions & Answers