functions/Find-DbaAgentJob.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
function Find-DbaAgentJob {
    <#
  .SYNOPSIS
   Find-DbaAgentJob finds agent job/s 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.
 
  .PARAMETER SqlInstance
   The SQL Server instance. You must have sysadmin access and server version must be SQL Server version 2000 or higher.
 
  .PARAMETER SqlCredential
   Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted.
 
  .PARAMETER LastUsed
   Find all jobs that havent ran in the INT number of previous day(s)
 
  .PARAMETER Disabled
   Find all jobs that are disabled
 
  .PARAMETER Failed
   Find all jobs that have failed
 
  .PARAMETER NoSchedule
   Find all jobs with schedule set to it
 
  .PARAMETER NoEmailNotification
   Find all jobs without email notification configured
 
  .PARAMETER Exclude
   Allows you to enter an array of agent job names to ignore
 
  .PARAMETER Name
   Filter agent jobs to only the names you list. This is a regex pattern by default so no asterisks are necessary. If you need an exact match, use -Exact.
 
  .PARAMETER Category
   Filter based on agent job categories
 
  .PARAMETER Owner
   Filter based on owner of the job/s
 
  .PARAMETER StepName
   Filter based on StepName. This is a regex pattern by default so no asterisks are necessary. If you need an exact match, use -Exact.
 
  .PARAMETER Exact
   Job Names and Step Names are searched for by regex by default. Use Exact to return only exact matches.
 
  .PARAMETER Since
   Datetime object used to narrow the results to a date
 
  .PARAMETER Silent
   Use this switch to disable any kind of verbose messages
 
  .NOTES
   Tags: Agent, Job
   Author: Stephen Bennett (https://sqlnotesfromtheunderground.wordpress.com/)
 
   Website: https://dbatools.io
   Copyright: (C) Chrissy LeMaire, clemaire@gmail.com
   License: GNU GPL v3 https://opensource.org/licenses/GPL-3.0
 
  .LINK
   https://dbatools.io/Find-DbaAgentJob
 
  .EXAMPLE
   Find-DbaAgentJob -SqlInstance Dev01 -Name backup
 
   Returns all agent job(s) that have backup in the name
 
  .EXAMPLE
   Find-DbaAgentJob -SqlInstance Dev01 -LastUsed 10
 
   Returns all agent job(s) that have not ran in 10 days
 
  .EXAMPLE
   Find-DbaAgentJob -SqlInstance Dev01 -Disabled -NoEmailNotification -NoSchedule
 
   Returns all agent job(s) that are either disabled, have no email notification or dont have a schedule. returned with detail
 
  .EXAMPLE
   Find-DbaAgentJob -SqlInstance Dev01 -LastUsed 10 -Exclude "Yearly - RollUp Workload", "SMS - Notification"
 
   Returns all agent jobs that havent ran in the last 10 ignoring jobs "Yearly - RollUp Workload" and "SMS - Notification"
 
  .EXAMPLE
   Find-DbaAgentJob -SqlInstance Dev01 -Category "REPL-Distribution", "REPL-Snapshot" -Detailed | Format-Table -AutoSize -Wrap
 
   Returns all job/s on Dev01 that are in either category "REPL-Distribution" or "REPL-Snapshot" with detailed output
 
  .EXAMPLE
   Find-DbaAgentJob -SqlInstance Dev01, Dev02 -Failed -Since '7/1/2016 10:47:00'
 
   Returns all agent job(s) that have failed since July of 2016 (and still have history in msdb)
 
  .EXAMPLE
   Get-DbaRegisteredServerName -SqlInstance CMSServer -Group Production | Find-DbaAgentJob -Disabled -NoSchedule -Detailed | 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
   Find-DbaAgentJob -SqlInstance Dev01, Dev02 -Name Mybackup -Exact
 
   Returns all agent job(s) that are named exactly Mybackup
 #>

    [CmdletBinding()]
    Param (
        [parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $True)]
        [Alias("ServerInstance", "SqlServer", "SqlServers")]
        [DbaInstanceParameter[]]$SqlInstance,
        [PSCredential]
        $SqlCredential,
        [string[]]$Name,
        [string[]]$StepName,
        [switch]$Exact,
        [int]$LastUsed,
        [switch]$Disabled,
        [switch]$Failed,
        [switch]$NoSchedule,
        [switch]$NoEmailNotification,
        [string[]]$Category,
        [string]$Owner,
        [string[]]$Exclude,
        [datetime]$Since,
        [switch]$Silent
    )
    begin {
        if ($Failed, [boolean]$Name, [boolean]$StepName, [boolean]$LastUsed.ToString(), $Disabled, $NoSchedule, $NoEmailNotification, [boolean]$Category, [boolean]$Owner, [boolean]$Exclude -notcontains $true) {
            Stop-Function -Message "At least one search term must be specified"
        }
    }
    process {
        if (Test-FunctionInterrupt) { return }

        foreach ($instance in $SqlInstance) {
            Write-Message -Level Verbose -Message "Running Scan on: $instance"

            try {
                Write-Message -Level Verbose -Message "Connecting to $instance"
                $server = Connect-SqlInstance -SqlInstance $instance -SqlCredential $sqlcredential
            }
            catch {
                Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue
            }

            $jobs = $server.JobServer.jobs
            $output = @()

            if ($Failed) {
                Write-Message -Level Verbose -Message "Checking for failed jobs"
                $output += $jobs | Where-Object { $_.LastRunOutcome -eq "Failed" }
            }

            if ($Name) {
                foreach ($jobname in $Name) {
                    Write-Message -Level Verbose -Message "Gettin some jobs by their names"
                    if ($Exact -eq $true) {
                        $output += $jobs | Where-Object { $_.Name -eq $name }
                    }
                    else {
                        try {
                            $output += $jobs | Where-Object { $_.Name -match $name }
                        }
                        catch {
                            # they prolly put aterisks thinking it's a like
                            $Name = $Name -replace '\*', ''
                            $Name = $Name -replace '\%', ''
                            $output += $jobs | Where-Object { $_.Name -match $name }
                        }
                    }
                }
            }

            if ($StepName) {
                foreach ($name in $StepName) {
                    Write-Message -Level Verbose -Message "Gettin some jobs by their names"
                    if ($Exact -eq $true) {
                        $output += $jobs | Where-Object { $_.JobSteps.Name -eq $name }
                    }
                    else {
                        try {
                            $output += $jobs | Where-Object { $_.JobSteps.Name -match $name }
                        }
                        catch {
                            # they prolly put aterisks thinking it's a like
                            $StepName = $StepName -replace '\*', ''
                            $StepName = $StepName -replace '\%', ''
                            $output += $jobs | Where-Object { $_.JobSteps.Name -match $name }
                        }
                    }
                }
            }

            if ($LastUsed) {
                $DaysBack = $LastUsed * -1
                $SinceDate = (Get-date).AddDays($DaysBack)
                Write-Message -Level Verbose -Message "Finding job/s not ran in last $LastUsed days"
                $output += $jobs | Where-Object { $_.LastRunDate -le $SinceDate }
            }

            if ($Disabled) {
                Write-Message -Level Verbose -Message "Finding job/s that are disabled"
                $output += $jobs | Where-Object { $_.IsEnabled -eq $false }
            }

            if ($NoSchedule) {
                Write-Message -Level Verbose -Message "Finding job/s that have no schedule defined"
                $output += $jobs | Where-Object { $_.HasSchedule -eq $false }
            }
            if ($NoEmailNotification) {
                Write-Message -Level Verbose -Message "Finding job/s that have no email operator defined"
                $output += $jobs | Where-Object { $_.OperatorToEmail -eq "" }
            }


            if ($Category) {
                Write-Message -Level Verbose -Message "Finding job/s that have the specified category defined"
                $output += $jobs | Where-Object { $Category -contains $_.Category }
            }

            if ($Owner) {
                Write-Message -Level Verbose -Message "Finding job/s with owner critera"
                if ($Owner -match "-") {
                    $OwnerMatch = $Owner -replace "-", ""
                    Write-Message -Level Verbose -Message "Checking for jobs that NOT owned by: $OwnerMatch"
                    $output += $server.JobServer.jobs | Where-Object { $OwnerMatch -notcontains $_.OwnerLoginName }
                }
                else {
                    Write-Message -Level Verbose -Message "Checking for jobs that are owned by: $owner"
                    $output += $server.JobServer.jobs | Where-Object { $Owner -contains $_.OwnerLoginName }
                }
            }

            if ($Exclude) {
                Write-Message -Level Verbose -Message "Excluding job/s based on Exclude"
                $output = $output | Where-Object { $Exclude -notcontains $_.Name }
            }

            if ($Since) {
                #$Since = $Since.ToString("yyyy-MM-dd HH:mm:ss")
                Write-Message -Level Verbose -Message "Getting only jobs whose LastRunDate is greater than or equal to $since"
                $output = $output | Where-Object { $_.LastRunDate -ge $since }
            }

            $jobs = $output | Select-Object -Unique

            foreach ($job in $jobs) {
                Add-Member -Force -InputObject $job -MemberType NoteProperty -Name ComputerName -value $server.NetName
                Add-Member -Force -InputObject $job -MemberType NoteProperty -Name InstanceName -value $server.ServiceName
                Add-Member -Force -InputObject $job -MemberType NoteProperty -Name SqlInstance -value $server.DomainInstanceName
                $job | Select-DefaultView -Property ComputerName, InstanceName, SqlInstance, Name, LastRunDate, LastRunOutcome, IsEnabled, CreateDate, HasSchedule, OperatorToEmail, Category, OwnerLoginName
            }
        }
    }
}