Script Repository


All errors

April 11, 2017
1579

The script e-mails an HTML report on all errors that occurred during a predefined number of days.

Note: The script uses the $Context variable available on the server side only. This means that it 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 = "Adaxes errors for the last $numDays days" # TODO: modify me
$reportHeader = @"
<b>Adaxes errors for 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 Service Log
$path = $Context.GetWellKnownContainerPath("ServiceLog")
$serviceLog = $Context.BindToObject($path)

# Set start and end dates
$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)

$errorCount = 0
foreach ($record in $records)
{
    if (($record.State -eq "OPERATION_STATE_FAILED_CAN_CONTINUE") -or ($record.State -eq "OPERATION_STATE_FAILED_NO_CONTINUE"))
    {
        # Add log record to report
        $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 the report
            $executionLog = GetExecutionLog $executionLogEntries ""
        }
        $reportHeader += "<tr valign='top'><td>$recordStartTime</td><td>$recordCompletionTime</td><td>$recordInitiatorName</td><td>$recordTargetObjectName</td><td>$recordTargetObjectType</td><td>$recordDescription</td><td>$executionLog</td></tr>"
        
        $errorCount++
    }
}

if ($errorCount -ne 0)
{
    # Build report
    $report = $reportHeader + "</table><p>Total Errors: $errorCount</p>" + $reportFooter
    
    # Send report
    $Context.SendMail($to, $subject, $NULL, $report)
}


Comments ( 0 )
No results found.
Leave a comment