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

Top 10 Approvers

November 22, 2023 Views: 1880

The script creates and emails an HTML-formatted report on top 10 users who approved more operations than the others.

Note: The script uses the $Context variable available on the server side only. This means that the script can be executed only by business rules, custom commands, and scheduled tasks. For example, to schedule the report, you can create a scheduled task configured for the Domain object type. To add the script to a scheduled task, use the Run a program or PowerShell script action.

Parameters:

  • $to - Specifies email addresses of the recipient(s) of the report.
  • $subject - Specifies the email message subject.
  • $reportHeader - Specifies the email message header.
  • $reportFooter - Specifies the email message footer.
Edit Remove
PowerShell
# E-mail message settings
$to = "recipient@domain.com" # TODO: modify me
$subject = "Top 10 Approvers" # TODO: modify me
$reportHeader = "<h2><b>Top 10 Approvers</b></h2>" # TODO: modify me
$reportFooter = "<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

function GetDisplayName($path)
{
    return $Context.GetDisplayNameFromAdsPath($path)
}

# FInd all approved requests
$containerPath = $Context.GetWellKnownContainerPath("ApprovalRequests")
$searcher = $Context.BindToObject($containerPath)
$searcher.SearchFilter = "(&(objectClass=adm-ApprovalRequest)(adm-ApprovalState=1))"
$searcher.PageSize = 500
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.SetPropertiesToLoad(@("adm-ProcessedByGuid"))

try
{
    $searchResult = $searcher.ExecuteSearch()
    $requests = $searchResult.FetchAll()
    
    $approverInfo = @{}
    foreach ($requestId in $requests)
    {
        $approverGuid = [Guid]$requestId.Properties["adm-ProcessedByGuid"].Value
        if ($approverInfo.ContainsKey($approverGuid))
        {
            $approverInfo[$approverGuid] += 1
        }
        else
        {
            $approverInfo.Add($approverGuid, 1) | Out-Null
        }
    }
}
finally
{
    $searchResult.Dispose()
}

# Get top 10 approvers
$result = $approverInfo.GetEnumerator() | Sort value -Descending | Select -First 10

# Get the default Web Interface address
$webInterfaceAddress = "%adm-WebInterfaceUrl%"
if ([System.String]::IsNullOrEmpty($webInterfaceAddress))
{
    $Context.LogMessage("Default web interface address not set for Adaxes service. For details, see http://www.adaxes.com/help/?HowDoI.ManageService.RegisterWebInterface.html", "Warning")
}

# Build report
$reportHeader += "<ol>"
foreach ($entry in $result)
{
    $approverGuid = $entry.Key
    $path = "Adaxes://<GUID=$approverGuid>"
    
    # Add approver info to the report
    $approver = $Context.BindToObject($path)
    $approverDisplayName = GetDisplayName $approver.AdsPath
    $processedRequests = $entry.Value
    $reportHeader += "<li><a href='$webInterfaceAddress`#/Browse/$approverGuid'>$approverDisplayName</a>; <br/>Processed requests: $processedRequests</li>"
}
$reportHeader += "</ol>"

# Send mail
$report = $reportHeader + $reportFooter
$Context.SendMail($to, $subject, $NULL, $report)

Comments 0
Leave a comment
Loading...

Got questions?

Support Questions & Answers