looking for info on the below piece of code, it actually does what's expected of it. It retrieves the time duration of the 'CreateTimesheets' task for the past 7 days. However it finishes with an error message.
Get-WinEvent -FilterHashtable @{
'LogName' = 'Microsoft-Windows-TaskScheduler/Operational'
'ID' = 200, 201
'StartTime' = [datetime]::Today.AddDays(-7)
'EndTime' = [datetime]::Today
} | Group-Object ActivityID | ForEach-Object {
if($_.Group.Properties[0].Value -like '*CreateTimesheets*'){
$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
'StartTime' = $start
'Duration' = ($end - $start).TotalSeconds
} }
}
The error message is as follows
Cannot find an overload for "op_Subtraction" and the argument count: "2".
At line:15 char:12
+ New-Object -Type PSObject -Property @{
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
i'm quite new to powershell scripting and have found the same error message on other stack overflow questions but the scenarios seem quite different to this, could anyone clear this up for me? thanks
My Guess is you have a grouped ActivityID without a matching 200 / 201 ID. (Task that didn't finish during the time you ran your script)
If you only want to tasks that have finished and have an $end value - see if modifying line 6 like this does the trick:
} | Group-Object ActivityID | Where-Object {$_.Count -gt 1} | ForEach-Object {