0

Tried running bellow command but "Duration" filter fails.

Get-DbaAgentJob -SqlInstance instance_name | `
| Get-DbaAgentJobHistory -StartDate '2022-08-02' -EndDate '2022-08-03' `
| Where-Object Duration -gt [TimeSpan]'00:05:00'

Error message:

Where-Object : The 'Igt' operator failed: Could not compare "00:01:32" to "[TimeSpan]00:05:00". Error: "Cannot convert value "[TimeSpan]00:05:00" to type "Sqlcollaborative.Dbatools.Utility.DbaTimeSpanPretty". Error: "Failed to parse as timespan: [TimeSpan]00:05:00 at [TimeSpan]00:05:00"". At line:3 char:7 | Where-Object Duration -gt [TimeSpan]'00:05:00' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CategoryInfo : InvalidOperation: (System.Data.DataRow:PSObject) [Where-Object], PSInvalidOperationException FullyQualifiedErrorId : OperatorFailed,Microsoft.PowerShell.Commands.WhereObjectCommand

  • Maybe just do `Where-Object Duration -gt '00:05:00'` – Charlieface Aug 03 '22 at 19:27
  • Thanks. Get an error without timespan casting: Where-Object : The argument to operator 'Igt' is not valid: Object must be of type TimeSpan.. At line:3 char:1 Where-Object Duration -gt '00:05:00' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CategoryInfo : InvalidArgument: (System.Data.DataRow:PSObject) [Where-Object], PSArgumentExce ption FullyQualifiedErrorId : BadOperatorArgument,Microsoft.PowerShell.Commands.WhereObjectCommand – Daniel Bezerra Aug 05 '22 at 10:36

1 Answers1

0

Just got an alternative:

USE msdb
GO

SELECT 
    j.name AS 'JobName',
    s.step_id AS 'Step',
    s.step_name AS 'StepName',
    msdb.dbo.agent_datetime(run_date, run_time) AS 'RunDateTime',
    ((run_duration/10000*3600 + (run_duration/100)%100*60 + run_duration%100 + 31 ) / 60) AS 'RunDurationMinutes'
FROM msdb.dbo.sysjobs j 
INNER JOIN msdb.dbo.sysjobsteps s ON j.job_id = s.job_id
INNER JOIN msdb.dbo.sysjobhistory h ON s.job_id = h.job_id 
    AND s.step_id = h.step_id 
    AND h.step_id <> 0
WHERE ((run_duration/10000*3600 + (run_duration/100)%100*60 + run_duration%100 + 31 ) / 60) >= 5
    AND msdb.dbo.agent_datetime(run_date, run_time) BETWEEN '2022-08-03 09:00:00' AND '2022-08-03 10:30:00'  --Uncomment for date range queries
    --AND j.name = 'TestJob' --Uncomment to search for a single job
ORDER BY RunDurationMinutes DESC, RunDateTime