Script repository

Scheduled task activity history

Updated on: Jan 18, 2026, Views: 5262

Export data

The script emails a report containing the actions performed by a scheduled task. To execute the script, create a scheduled task configured for the Domain object type and add a managed domain to the Activity Scope of the task. The domain will only be used to trigger execution of the scheduled task.

Parameters

  • $taskName - the name of the scheduled task.
  • $numDays - the number of days to include activity history for. Set to 0 to include all log records.
  • $to - the email address of the email notification recipient.
  • $subject - the email notification subject.
  • $reportHeader - the email notification header.
  • $reportFooter - the email notification footer.
$taskName = "Inactive User Deleter" # TODO: modify me
$numDays = 1 # TODO: modify me
$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.
        $subEntries = $logEntry.SubEntries
        if ($subEntries.Count -ne 0)
        {
            $executionLog = GetExecutionLog $subEntries $executionLog
        }
        $executionLog += "</li>"
    }
    $executionLog += "</ul>"
    return $executionLog
}

# Bind to 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 mail
$Context.SendMail($to, $subject, $NULL, $messageBody)

Comments 0

You must be signed in to comment.

    Got questions?

    Support Questions & Answers

    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.