commands

^

Find-DbaAgentJob

Author Stephen Bennett (https://sqlnotesfromtheunderground.wordpress.com/)
Availability Windows, Linux, macOS

 

Synopsis

Find-DbaAgentJob finds agent jobs that fit certain search filters.

Description

This command filters SQL Agent jobs giving the DBA a list of jobs that may need attention or could possibly be options for removal.

Syntax

Find-DbaAgentJob
    [-SqlInstance] <DbaInstanceParameter[]>
    [[-SqlCredential] <PSCredential>]
    [[-JobName] <String[]>]
    [[-ExcludeJobName] <String[]>]
    [[-StepName] <String[]>]
    [[-LastUsed] <Int32>]
    [-IsDisabled]
    [-IsFailed]
    [-IsNotScheduled]
    [-IsNoEmailNotification]
    [[-Category] <String[]>]
    [[-Owner] <String>]
    [[-Since] <DateTime>]
    [-EnableException]
    [<CommonParameters>]

 

Examples

 

Example: 1
PS C:\> Find-DbaAgentJob -SqlInstance Dev01 -JobName *backup*

Returns all agent job(s) that have backup in the name

Example: 2
PS C:\> Find-DbaAgentJob -SqlInstance Dev01, Dev02 -JobName Mybackup

Returns all agent job(s) that are named exactly Mybackup

Example: 3
PS C:\> Find-DbaAgentJob -SqlInstance Dev01 -LastUsed 10

Returns all agent job(s) that have not ran in 10 days

Example: 4
PS C:\> Find-DbaAgentJob -SqlInstance Dev01 -IsDisabled -IsNoEmailNotification -IsNotScheduled

Returns all agent job(s) that are either disabled, have no email notification or don't have a schedule. returned with detail

Example: 5
PS C:\> $servers | Find-DbaAgentJob -IsFailed | Start-DbaAgentJob

Finds all failed job then starts them. Consider using a -WhatIf at the end of Start-DbaAgentJob to see what it'll do first

Example: 6
PS C:\> Find-DbaAgentJob -SqlInstance Dev01 -LastUsed 10 -Exclude "Yearly - RollUp Workload", "SMS - Notification"

Returns all agent jobs that have not ran in the last 10 days ignoring jobs "Yearly - RollUp Workload" and "SMS - Notification"

Example: 7
PS C:\> Find-DbaAgentJob -SqlInstance Dev01 -Category "REPL-Distribution", "REPL-Snapshot" | Format-Table -AutoSize -Wrap

Returns all job/s on Dev01 that are in either category "REPL-Distribution" or "REPL-Snapshot"

Example: 8
PS C:\> Find-DbaAgentJob -SqlInstance Dev01, Dev02 -IsFailed -Since '2016-07-01 10:47:00'

Returns all agent job(s) on Dev01 and Dev02 that have failed since July of 2016 (and still have history in msdb)

Example: 9
PS C:\> Get-DbaRegServer -SqlInstance CMSServer -Group Production | Find-DbaAgentJob -Disabled -IsNotScheduled | Format-Table -AutoSize -Wrap

Queries CMS server to return all SQL instances in the Production folder and then list out all agent jobs that have either been disabled or have no schedule.

Example: 10
PS C:\> $Instances = 'SQL2017N5','SQL2019N5','SQL2019N20','SQL2019N21','SQL2019N22'

Find-DbaAgentJob -SqlInstance $Instances -JobName backup -IsNotScheduled
Returns all agent job(s) wiht backup in the name, that don't have a schedule on 'SQL2017N5','SQL2019N5','SQL2019N20','SQL2019N21','SQL2019N22'

Required Parameters

-SqlInstance

The target SQL Server instance or instances. You must have sysadmin access and server version must be SQL Server version 2000 or higher.

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
-JobName

Filter agent jobs to only the name(s) you list. Supports regular expression (e.g. MyJob*) being passed in.

Alias Name
Required False
Pipeline false
Default Value
-ExcludeJobName

Allows you to enter an array of agent job names to ignore

Alias
Required False
Pipeline false
Default Value
-StepName

Filter based on StepName. Supports regular expression (e.g. MyJob*) being passed in.

Alias
Required False
Pipeline false
Default Value
-LastUsed

Find all jobs that haven't ran in the INT number of previous day(s)

Alias
Required False
Pipeline false
Default Value 0
-IsDisabled

Find all jobs that are disabled

Alias Disabled
Required False
Pipeline false
Default Value False
-IsFailed

Find all jobs that have failed

Alias Failed
Required False
Pipeline false
Default Value False
-IsNotScheduled

Find all jobs with no schedule assigned

Alias NoSchedule
Required False
Pipeline false
Default Value False
-IsNoEmailNotification

Find all jobs without email notification configured

Alias NoEmailNotification
Required False
Pipeline false
Default Value False
-Category

Filter based on agent job categories

Alias
Required False
Pipeline false
Default Value
-Owner

Filter based on owner of the job/s

Alias
Required False
Pipeline false
Default Value
-Since

Datetime object used to narrow the results to a date

Alias
Required False
Pipeline false
Default Value
-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 Find-DbaAgentJob on GitHub.
Want to see the Bill Of Health for this command? Check out Find-DbaAgentJob.