Hello,
Do we understand correctly, that you need a report that will email alerts only for PowerShell scripts executed by a Scheduled Task? If you do, find the updated script below:
$taskName = "Inactive User Deleter" # TODO: modify me
$numDays = 1 # set to 0 to output all log records
$operationTypes = @("run script") # TODO: modify me. Values: $NULL or Operation types array
$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
}
# Check operation
if ($operationTypes -ne $NULL)
{
$recordOperationTypes = $record.GetOperationTypes()
$result = Compare-Object -ReferenceObject $recordOperationTypes -DifferenceObject $operationTypes -IncludeEqual -ExcludeDifferent
if ($result -eq $NULL)
{
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)
If you need a report to email other information, please provide us with as much details per your request as you can.