Script Repository


Notify users on Requests awaiting their approval

October 04, 2017
953

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`ViewObject.aspx?guid=$requestGuid'>$requestOperation</a></li>"
    }
    $unapprovedTaskList += "</ol>"
    
    # Build email notification
    $htmlBody = $notificationHeader + $unapprovedTaskList + $notificationFooter
    
    # Send notification to approver
    $Context.SendMail($mail, $subject, $NULL, $htmlBody)
}


Comments ( 0 )
No results found.
Leave a comment