The script generates a report containing all scheduled tasks, their last run date and duration. To execute the script, create a report with corresponding custom columns. The report should have no scope or parameters.
Parameters:
- $lastStartTimeColumnID - Specifies the identifier of the custom column that will contain the time a scheduled task run was last started.
- $lastRunDurationColumnID - Specifies the identifier of the custom column that will contain the duration (in seconds) the scehduled task last ran for. To get the identifier of a custom column:
- In the Report-specific columns section, on the Columns tab, right-click the custom column.
- In the context menu, navigate to Copy and click Column ID.
- The column identifier will be copied to clipboard.
PowerShell
$lastStartTimeColumnID = "{82112c4b-7cdf-4d58-9bd8-0c1af2eff9d2}" # TODO: modify me
$lastRunDurationColumnID = "{a881584d-005b-45c8-a9e1-4c8eb58ff901}" # TODO: modify me
# Set search criteria
$criteria = New-AdmCriteria "adm-ScheduledTask"
$Context.DirectorySearcher.AddCriteria($criteria)
# Set search base object
$scheduledTasksContainerPath = $Context.GetWellKnownContainerPath("ScheduledTasks")
$Context.DirectorySearcher.BaseObjectPath = $scheduledTasksContainerPath
try
{
# Execute search
$searchResultIterator = $Context.DirectorySearcher.ExecuteSearch()
$searchResults = $searchResultIterator.FetchAll()
foreach ($searchResult in $searchResults)
{
# Bind to the scheduled task
$scheduledTask = $Context.BindToObjectBySearchResultEx($searchResult, $True)
# Get last run duration in seconds
$durationTimeSpan = New-TimeSpan –Start $scheduledTask.LastRunStartDateTime –End $scheduledTask.LastRunFinishDateTime
$duration = [int]($durationTimeSpan.TotalSeconds)
# Add to report
$Context.Items.Add($searchResult, @{$lastStartTimeColumnID = $scheduledTask.LastRunStartDateTime; $lastRunDurationColumnID = $duration}, $NULL)
}
}
finally
{
# Release resources
if ($searchResultIterator){ $searchResultIterator.Dispose() }
}