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 tasks with last run start time and duration

August 06, 2024 Views: 144

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:
    1. In the Report-specific columns section, on the Columns tab, right-click the custom column.
    2. In the context menu, navigate to Copy and click Column ID.
    3. The column identifier will be copied to clipboard.
Edit Remove
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() }
}
Comments 0
Leave a comment
Loading...

Got questions?

Support Questions & Answers