Script Repository


Operations performed over a predefined number of days

September 16, 2015
1188

This script e-mails an HTML report on all operations performed during a predefined number of days. Information on each operation also includes its Execution Log.

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-DNS object type. To add the script to a 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 the recipient 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 = 1 # set to 0 to output all records
$to = "recipient@domain.com" # TODO: modify me
$subject = "Operations performed during the last $numDays days" # TODO: modify me
$reportHeader = @"
<b>Operations performed during the last $numDays days</b><br/><br/>
<table border="1">
    <tr>
        <th>Start Time</th>
        <th>Completion Time</th>
        <th>Initiator</th>
        <th>Target Object</th>
        <th>Target Object Type</th>
        <th>Operation</th>
        <th>Execution Log</th>
    </tr>
"@ # 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 GetExecutionLog ($logEntryCollection, $executionLog)
{
    $executionLog += "<ul>"
    foreach ($logEntry in $logEntryCollection)
    {
        # Get operation info
        $type = $logEntry.Type
        $message = $logEntry.Message
        $source = $logEntry.Source
        
        # Build report record
        $messageBuilder = ""
        if (-not([System.String]::IsNullOrEmpty($source)))
        {
            # Add source to the message
            $messageBuilder += "$source`: "
        }
        $messageBuilder += "$type - $message"
        
        # Encode all HTML tags
        $messageBuilder = [System.Web.HttpUtility]::HtmlEncode($messageBuilder)
        
        # Add message to report
        $executionLog += "<li>$messageBuilder"
        
        # Add subentries, if any
        $subEntries = $logEntry.SubEntries
        if ($subEntries.Count -ne 0)
        {
            $executionLog = GetExecutionLog $subEntries $executionLog
        }
        $executionLog += "</li>"
    }
    $executionLog += "</ul>"
    return $executionLog
}

# Bind to General Log
$path = $Context.GetWellKnownContainerPath("ServiceLog")
$serviceLog = $Context.BindToObject($path)
$generalLog = $serviceLog.GeneralLog
if ($numDays -ne 0)
{
   $generalLog.StartDateTime = (Get-Date).AddDays(-$numDays)
   $generalLog.EndDateTime = Get-Date
}

# Get log records
$log = $generalLog.Log
$records = $log.GetPage(0)

# Add log records to report
foreach ($record in $records)
{
    if ($record.State -eq "OPERATION_STATE_FAILED_NO_CONTINUE")
    {
        $reportRecord = "<tr bgcolor='red' valign='top'>"
    }
    elseIf ($record.State -eq "OPERATION_STATE_FAILED_CAN_CONTINUE")
    {
        $reportRecord = "<tr bgcolor='yellow' valign='top'>"
    }
    else
    {
        $reportRecord = "<tr valign='top'>"
    }
    $recordStartTime = $record.StartTime 
    $recordCompletionTime = $record.CompletionTime
    $recordInitiatorName = $record.Initiator.Name
    $recordTargetObjectName = $record.TargetObjectName
    $recordTargetObjectType = $record.TargetObjectType
    $recordDescription = $record.Description
    
    # Get Execution Log
    $executionLogEntries = $record.GetExecutionLog()
    if ($executionLogEntries.Count -eq 0)
    {
        $executionLog = "Execution Log is empty"
    }
    else
    {
        # Add Execution Log to report
        $executionLog = GetExecutionLog $executionLogEntries ""
    }
    $reportRecord += "<td>$recordStartTime</td><td>$recordCompletionTime</td><td>$recordInitiatorName</td><td>$recordTargetObjectName</td><td>$recordTargetObjectType</td><td>$recordDescription</td><td>$executionLog</td>"
    $reportRecord += "</tr>"
    
    # Add record to report
    $reportHeader += $reportRecord
}
$reportHeader += "</table>"
$report = $reportHeader + $reportFooter

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

Comments ( 0 )
No results found.
Leave a comment