commands

^

Get-DbaAgentJobHistory

Author Klaas Vandenberghe (@PowerDbaKlaas) , Simone Bizzotto (@niphold)
Availability Windows, Linux, macOS

 

Synopsis

Gets execution history of SQL Agent Job on instance(s) of SQL Server.

Description

Get-DbaAgentJobHistory returns all information on the executions still available on each instance(s) of SQL Server submitted. The cleanup of SQL Agent history determines how many records are kept.

https://msdn.microsoft.com/en-us/library/ms201680.aspx https://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.agent.jobhistoryfilter(v=sql.120).aspx

Syntax

Get-DbaAgentJobHistory
    [-SqlCredential <PSCredential>]
    [-Job <Object[]>]
    [-ExcludeJob <Object[]>]
    [-StartDate <DateTime>]
    [-EndDate <DateTime>]
    [-OutcomeType {Failed | Succeeded | Retry | Cancelled | InProgress | Unknown}]
    [-ExcludeJobSteps]
    [-WithOutputFile]
    [-EnableException]
    [<CommonParameters>]

Get-DbaAgentJobHistory -SqlInstance <DbaInstanceParameter[]>
    [-SqlCredential <PSCredential>]
    [-Job <Object[]>]
    [-ExcludeJob <Object[]>]
    [-StartDate <DateTime>]
    [-EndDate <DateTime>]
    [-OutcomeType {Failed | Succeeded | Retry | Cancelled | InProgress | Unknown}]
    [-ExcludeJobSteps]
    [-WithOutputFile]
    [-EnableException]
    [<CommonParameters>]

Get-DbaAgentJobHistory
    [-SqlCredential <PSCredential>]
    [-Job <Object[]>]
    [-ExcludeJob <Object[]>]
    [-StartDate <DateTime>]
    [-EndDate <DateTime>]
    [-OutcomeType {Failed | Succeeded | Retry | Cancelled | InProgress | Unknown}]
    [-ExcludeJobSteps]
    [-WithOutputFile] -JobCollection <Job>
    [-EnableException]
    [<CommonParameters>]

 

Examples

 

Example: 1
PS C:\> Get-DbaAgentJobHistory -SqlInstance localhost

Returns all SQL Agent Job execution results on the local default SQL Server instance.

Example: 2
PS C:\> Get-DbaAgentJobHistory -SqlInstance localhost, sql2016

Returns all SQL Agent Job execution results for the local and sql2016 SQL Server instances.

Example: 3
PS C:\> 'sql1','sql2\Inst2K17' | Get-DbaAgentJobHistory

Returns all SQL Agent Job execution results for sql1 and sql2\Inst2K17.

Example: 4
PS C:\> Get-DbaAgentJobHistory -SqlInstance sql2\Inst2K17 | Select-Object *

Returns all properties for all SQl Agent Job execution results on sql2\Inst2K17.

Example: 5
PS C:\> Get-DbaAgentJobHistory -SqlInstance sql2\Inst2K17 -Job 'Output File Cleanup'

Returns all properties for all SQl Agent Job execution results of the 'Output File Cleanup' job on sql2\Inst2K17.

Example: 6
PS C:\> Get-DbaAgentJobHistory -SqlInstance sql2\Inst2K17 -Job 'Output File Cleanup' -WithOutputFile

Returns all properties for all SQl Agent Job execution results of the 'Output File Cleanup' job on sql2\Inst2K17,
with additional properties that show the output filename path

Example: 7
PS C:\> Get-DbaAgentJobHistory -SqlInstance sql2\Inst2K17 -ExcludeJobSteps

Returns the SQL Agent Job execution results for the whole jobs on sql2\Inst2K17, leaving out job step execution results.

Example: 8
PS C:\> Get-DbaAgentJobHistory -SqlInstance sql2\Inst2K17 -StartDate '2017-05-22' -EndDate '2017-05-23 12:30:00'

Returns the SQL Agent Job execution results between 2017/05/22 00:00:00 and 2017/05/23 12:30:00 on sql2\Inst2K17.

Example: 9
PS C:\> Get-DbaAgentJob -SqlInstance sql2016 | Where-Object Name -Match backup | Get-DbaAgentJobHistory

Gets all jobs with the name that match the regex pattern "backup" and then gets the job history from those. You can also use -Like backup in this example.

Example: 10
PS C:\> Get-DbaAgentJobHistory -SqlInstance sql2016 -OutcomeType Failed

Returns only the failed SQL Agent Job execution results for the sql2016 SQL Server instance.

Required Parameters

-SqlInstance

The target SQL Server instance or instances. This can be a collection and receive pipeline input to allow the function to be executed against multiple SQL Server instances.

Alias
Required True
Pipeline true (ByValue)
Default Value
-JobCollection

An array of SMO jobs

Alias
Required True
Pipeline true (ByValue)
Default Value

Optional Parameters

-SqlCredential

Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential). Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported. For MFA support, please use Connect-DbaInstance.

Alias
Required False
Pipeline false
Default Value
-Job

The name of the job from which the history is wanted. If unspecified, all jobs will be processed.

Alias
Required False
Pipeline false
Default Value
-ExcludeJob

The job(s) to exclude - this list is auto-populated from the server

Alias
Required False
Pipeline false
Default Value
-StartDate

The DateTime starting from which the history is wanted. If unspecified, all available records will be processed.

Alias
Required False
Pipeline false
Default Value 1900-01-01
-EndDate

The DateTime before which the history is wanted. If unspecified, all available records will be processed.

Alias
Required False
Pipeline false
Default Value $(Get-Date)
-OutcomeType

The CompletionResult to filter the history for. Valid values are: Failed, Succeeded, Retry, Cancelled, InProgress, Unknown

Alias
Required False
Pipeline false
Default Value
Accepted Values Failed,Succeeded,Retry,Cancelled,InProgress,Unknown
-ExcludeJobSteps

Use this switch to discard all job steps, and return only the job totals

Alias
Required False
Pipeline false
Default Value False
-WithOutputFile

Use this switch to retrieve the output file (only if you want step details). Bonus points, we handle the quirks of SQL Agent tokens to the best of our knowledge (https://technet.microsoft.com/it-it/library/ms175575(v=sql.110).aspx)

Alias
Required False
Pipeline false
Default Value False
-EnableException

By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message. This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting. Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.

Alias
Required False
Pipeline false
Default Value False

 

Want to see the source code for this command? Check out Get-DbaAgentJobHistory on GitHub.
Want to see the Bill Of Health for this command? Check out Get-DbaAgentJobHistory.