Script Repository


Approval Requests approved during the last X days

January 11, 2017
1239

This script e-mails a report on Approval Requests approved during a predefined number of days.

To generate such a report upon request, you can create a Custom Command that runs the script. To schedule the reports, you need to create a Scheduled Task configured for the Domain-DNS object type and run it against any of your AD domains.

To add the script to a Custom Command or Scheduled Task, use the Run a program or PowerShell script action.

Parameters:

  • $numDays - specifies the number of days to include in the report;
  • $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
$numDays = 7 # TODO: modify me
$to = "recipient@example.com" # TODO: modify me
$subject = "Requests approved during the last $numDays days" # TODO: modify me
$reportHeader = "<b>Approved operations:</b><br/><br/>" # 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

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

# Get approved Approval Requests
$requests = $aprovalsContainer.GetApprovalRequests("ADM_APPROVALSTATE_APPROVED")

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

$sendMail = $False
$modificationDate = (Get-Date).AddDays(-$numDays)
$approvedTasks = "<ol>"

# Find recently modified requests
foreach ($requestID in $requests)
{
    $guid = New-Object "System.Guid" (,$requestID)
    $guid = $guid.ToString("B")
    $requestPath = "Adaxes://<GUID=$guid>"
    $request = $Context.BindToObject($requestPath)
    
    $requestModificationDate = ($request.Get("whenChanged")).ToLocalTime()

    if ($requestModificationDate -lt $modificationDate)
    {
        continue # Approved earlier
    }
    
    # Add requests to the message
    $requestOperation = $request.DescriptionOfOperationToApprove
    $processedBy = [Softerra.Adaxes.Utils.ObjectNameHelper]::GetObjectName($request.ProcessedBy, 'IncludeParentPath')
    $approvedTasks += "<li>Operation: $requestOperation<br/> Processed By: $processedBy<br/> Date: $requestModificationDate<br/><a href='$webInterfaceAddress`ViewObject.aspx?guid=$guid'>Details</a></li><br/>"
    
    $sendMail = $True
}
$approvedTasks += "</ol>"

if (-not($sendMail))
{
    $Context.LogMessage("No requests matching the criteria.", "Information") # TODO: Modify me
    return
}

# Build report
$report = $reportHeader + $approvedTasks + $reportFooter

# Send mail
$Context.SendMail($to, $subject, $NULL, $report)


Comments ( 0 )
No results found.
Leave a comment