Script repository

Scheduled tasks with last run start time and duration

Updated on: Jan 18, 2026, Views: 1066

Reports

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 - the identifier of the custom column that will contain the time a scheduled task run was last started.
  • $lastRunDurationColumnID - 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.
$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-TimeSpanStart $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() }
}

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.