Taking inspiration on Get informations on Windows Scheduled Task duration (execution time) we aim to filter on a set of tasks, instead of fetching all from task scheduler. something of this sort:
$logName = 'Microsoft-Windows-TaskScheduler/Operational'
$xPathFilter = @'
*[
System[(EventID=200 or EventID=201)] and
EventData[
Data[@Name="TaskName"] = "001_task_A_nightly"
]
]
'@
Get-WinEvent -LogName $logName -FilterXPath $xPathFilter | Group-Object ActivityID | ForEach-Object {
$start = $_.Group |
Where-Object { $_.Id -eq 200 } |
Select-Object -Expand TimeCreated -First 1
$end = $_.Group |
Where-Object { $_.Id -eq 201 } |
Select-Object -Expand TimeCreated -First 1
New-Object -Type PSObject -Property @{
'TaskName' = $_.Group[0].Properties[0].Value
'Duration' = ($end - $start).TotalSeconds
Is there a way to adapt this one above?