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

Scheduled Task Activity History

March 04, 2021 Views: 3898

The script generates and emails an HTML report on the activity of a scheduled task. 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:

  • $taskName - Specifies the name of the scheduled task.
  • $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
$taskName = "Inactive User Deleter" # TODO: modify me
$numDays = 1 # set to 0 to output all log records
$to = "recipient@domain.com" # TODO: modify me
$subject = "Actions performed by $taskName" # TODO: modify me
$reportHeader = @"
<b>Actions performed by $taskName during the last $numDays days</b><br/><br/>
<table border="1">
    <tr>
        <th>Start Time</th>
        <th>Completion Time</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 the operation info
        $type = $logEntry.Type
        $message = $logEntry.Message
        $source = $logEntry.Source
        
        # Build report entry
        $messageBuilder = ""
        if (-not([System.String]::IsNullOrEmpty($source)))
        {
            # Add source to the message
            $messageBuilder += "$source`: "
        }
        $messageBuilder += "$type - $message"
        
        # Encode HTML tags
        $messageBuilder = [System.Web.HttpUtility]::HtmlEncode($messageBuilder)
        
        # Add entry to the report
        $executionLog += "<li>$messageBuilder"
        
        # Add Execution Log, if any
        $subEntries = $logEntry.SubEntries
        if ($subEntries.Count -ne 0)
        {
            $executionLog = GetExecutionLog $subEntries $executionLog
        }
        $executionLog += "</li>"
    }
    $executionLog += "</ul>"
    return $executionLog
}

# Bind to the directory object representing the General Log
$path = $Context.GetWellKnownContainerPath("ServiceLog")
$serviceLog = $Context.BindToObject($path)
    
$generalLog = $serviceLog.GeneralLog

# Set start and end dates
if ($numDays -ne 0)
{
   $generalLog.StartDateTime = (Get-Date).AddDays(-$numDays)
   $generalLog.EndDateTime = Get-Date
}

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

# Add log records to the report
foreach ($record in $records)
{
    if ($record.Initiator.Name -ine $taskName)
    {
        continue
    }
    
    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'>"
    }

    # Get log record properties
    $recordStartTime = $record.StartTime 
    $recordCompletionTime = $record.CompletionTime
    $recordTargetObjectName = $record.TargetObjectName
    $recordTargetObjectType = $record.TargetObjectType
    $recordDescription = $record.Description
    
    # Add the 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 ""
    }

    $reportRecord += "<td>$recordStartTime</td><td>$recordCompletionTime</td><td>$recordTargetObjectName</td><td>$recordTargetObjectType</td><td>$recordDescription</td><td>$executionLog</td>"
    $reportRecord += "</tr>"
    
    # Add record to the report
    $reportHeader += $reportRecord
}
$reportHeader += "</table>"
$messageBody = $reportHeader + $reportFooter

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

Comments 0
Leave a comment
Loading...

Got questions?

Support Questions & Answers