Skip to content

Commit ea45d97

Browse files
authored
Find-DbaAgentJob, work with multiple wildcards (#9583)
1 parent 8abb18d commit ea45d97

File tree

3 files changed

+26
-39
lines changed

3 files changed

+26
-39
lines changed

private/functions/Get-JobList.ps1

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -69,59 +69,44 @@ function Get-JobList {
6969

7070
$jobs = $server.JobServer.Jobs
7171
if ( (Test-Bound 'JobFilter') -or (Test-Bound 'StepFilter') ) {
72-
if ($JobFilter.Count -gt 1) {
73-
if ($Not) {
74-
$jobs | Where-Object Name -NotIn $JobFilter
75-
} else {
76-
$jobs | Where-Object Name -In $JobFilter
77-
}
78-
} else {
79-
foreach ($job in $jobs) {
80-
if ($JobFilter -match '`*') {
72+
73+
foreach ($job in $jobs) {
74+
foreach ($jFilter in $JobFilter) {
75+
if ($jFilter -match '`*') {
8176
if ($Not) {
82-
$job | Where-Object Name -NotLike $JobFilter
77+
$job | Where-Object Name -NotLike $jFilter
8378
} else {
84-
$job | Where-Object Name -Like $JobFilter
79+
$job | Where-Object Name -Like $jFilter
8580
}
8681
} else {
8782
if ($Not) {
88-
$job | Where-Object Name -NE $JobFilter
83+
$job | Where-Object Name -NE $jFilter
8984
} else {
90-
$job | Where-Object Name -EQ $JobFilter
85+
$job | Where-Object Name -EQ $jFilter
9186
}
9287
}
93-
if ($StepFilter -match '`*') {
94-
if ($Not) {
95-
$stepFound = $job.JobSteps | Where-Object Name -NotLike $StepFilter
96-
if ($stepFound.Count -gt 0) {
97-
$job
98-
}
99-
} else {
100-
$stepFound = $job.JobSteps | Where-Object Name -Like $StepFilter
101-
if ($stepFound.Count -gt 0) {
102-
$job
103-
}
104-
}
105-
} elseif ($StepName.Count -gt 1) {
88+
}
89+
foreach ($sFilter in $StepFilter) {
90+
if ($sFilter -match '`*') {
10691
if ($Not) {
107-
$stepFound = $job.JobSteps | Where-Object Name -NotIn $StepName
92+
$stepFound = $job.JobSteps | Where-Object Name -NotLike $sFilter
10893
if ($stepFound.Count -gt 0) {
10994
$job
11095
}
11196
} else {
112-
$stepFound = $job.JobSteps | Where-Object Name -In $StepName
97+
$stepFound = $job.JobSteps | Where-Object Name -Like $sFilter
11398
if ($stepFound.Count -gt 0) {
11499
$job
115100
}
116101
}
117102
} else {
118103
if ($Not) {
119-
$stepFound = $job.JobSteps | Where-Object Name -NE $StepName
104+
$stepFound = $job.JobSteps | Where-Object Name -NE $sFilter
120105
if ($stepFound.Count -gt 0) {
121106
$job
122107
}
123108
} else {
124-
$stepFound = $job.JobSteps | Where-Object Name -EQ $StepName
109+
$stepFound = $job.JobSteps | Where-Object Name -EQ $sFilter
125110
if ($stepFound.Count -gt 0) {
126111
$job
127112
}

public/Find-DbaAgentJob.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ function Find-DbaAgentJob {
9393
Finds all failed job then starts them. Consider using a -WhatIf at the end of Start-DbaAgentJob to see what it'll do first
9494
9595
.EXAMPLE
96-
PS C:\> Find-DbaAgentJob -SqlInstance Dev01 -LastUsed 10 -Exclude "Yearly - RollUp Workload", "SMS - Notification"
96+
PS C:\> Find-DbaAgentJob -SqlInstance Dev01 -LastUsed 10 -ExcludeJobName "Yearly - RollUp Workload", "SMS - Notification"
9797
9898
Returns all agent jobs that have not ran in the last 10 days ignoring jobs "Yearly - RollUp Workload" and "SMS - Notification"
9999
@@ -222,9 +222,9 @@ function Find-DbaAgentJob {
222222
}
223223
}
224224

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

230230
if ($Since) {

tests/Find-DbaAgentJob.Tests.ps1

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,22 @@ Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
2424
$null = New-DbaAgentJob -SqlInstance $TestConfig.instance2 -Job 'dbatoolsci_testjob' -OwnerLogin 'sa'
2525
$null = New-DbaAgentJobStep -SqlInstance $TestConfig.instance2 -Job 'dbatoolsci_testjob' -StepId 1 -StepName 'dbatoolsci Failed' -Subsystem TransactSql -SubsystemServer $srvName.sn -Command "RAISERROR (15600,-1,-1, 'dbatools_error');" -CmdExecSuccessCode 0 -OnSuccessAction QuitWithSuccess -OnFailAction QuitWithFailure -Database master -RetryAttempts 1 -RetryInterval 2
2626
$null = Start-DbaAgentJob -SqlInstance $TestConfig.instance2 -Job 'dbatoolsci_testjob'
27-
$null = New-DbaAgentJob -SqlInstance $TestConfig.instance2 -Job 'dbatoolsci_testjob' -OwnerLogin 'sa'
28-
$null = New-DbaAgentJobStep -SqlInstance $TestConfig.instance2 -Job 'dbatoolsci_testjob' -StepId 1 -StepName 'dbatoolsci Failed' -Subsystem TransactSql -SubsystemServer $srvName.sn -Command "RAISERROR (15600,-1,-1, 'dbatools_error');" -CmdExecSuccessCode 0 -OnSuccessAction QuitWithSuccess -OnFailAction QuitWithFailure -Database master -RetryAttempts 1 -RetryInterval 2
27+
$null = New-DbaAgentJob -SqlInstance $TestConfig.instance2 -Job 'dbatoolsregr_testjob' -OwnerLogin 'sa'
28+
$null = New-DbaAgentJobStep -SqlInstance $TestConfig.instance2 -Job 'dbatoolsregr_testjob' -StepId 1 -StepName 'dbatoolsci Failed' -Subsystem TransactSql -SubsystemServer $srvName.sn -Command "RAISERROR (15600,-1,-1, 'dbatools_error');" -CmdExecSuccessCode 0 -OnSuccessAction QuitWithSuccess -OnFailAction QuitWithFailure -Database master -RetryAttempts 1 -RetryInterval 2
2929
$null = New-DbaAgentJobCategory -SqlInstance $TestConfig.instance2 -Category 'dbatoolsci_job_category' -CategoryType LocalJob
3030
$null = New-DbaAgentJob -SqlInstance $TestConfig.instance2 -Job 'dbatoolsci_testjob_disabled' -Category 'dbatoolsci_job_category' -Disabled
3131
$null = New-DbaAgentJobStep -SqlInstance $TestConfig.instance2 -Job 'dbatoolsci_testjob_disabled' -StepId 1 -StepName 'dbatoolsci Test Step' -Subsystem TransactSql -SubsystemServer $srvName.sn -Command 'SELECT * FROM master.sys.all_columns' -CmdExecSuccessCode 0 -OnSuccessAction QuitWithSuccess -OnFailAction QuitWithFailure -Database master -RetryAttempts 1 -RetryInterval 2
3232
}
3333
AfterAll {
34-
$null = Remove-DbaAgentJob -SqlInstance $TestConfig.instance2 -Job dbatoolsci_testjob, dbatoolsci_testjob_disabled -Confirm:$false
34+
$null = Remove-DbaAgentJob -SqlInstance $TestConfig.instance2 -Job dbatoolsci_testjob, dbatoolsregr_testjob, dbatoolsci_testjob_disabled -Confirm:$false
3535
$null = Remove-DbaAgentJobCategory -SqlInstance $TestConfig.instance2 -Category 'dbatoolsci_job_category' -Confirm:$false
3636
}
3737

3838
$results = Find-DbaAgentJob -SqlInstance $TestConfig.instance2 -Job dbatoolsci_testjob
3939
It "Should find a specific job" {
4040
$results.name | Should Be "dbatoolsci_testjob"
4141
}
42-
$results = Find-DbaAgentJob -SqlInstance $TestConfig.instance2 -Job *dbatoolsci* -Exclude dbatoolsci_testjob_disabled
42+
$results = Find-DbaAgentJob -SqlInstance $TestConfig.instance2 -Job *dbatoolsci* -ExcludeJobName dbatoolsci_testjob_disabled
4343
It "Should find a specific job but not an excluded job" {
4444
$results.name | Should Not Be "dbatoolsci_testjob_disabled"
4545
}
@@ -79,11 +79,13 @@ Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
7979
It "Should find jobs that are owned by sa" {
8080
$results | Should not be null
8181
}
82-
83-
8482
$results = Find-DbaAgentJob -SqlInstance $TestConfig.instance2 -IsFailed -Since '2016-07-01 10:47:00'
8583
It "Should find jobs that have been failed since July of 2016" {
8684
$results | Should not be null
8785
}
86+
$results = Find-DbaAgentJob -SqlInstance $TestConfig.instance2 -Job *dbatoolsci*,*dbatoolsregr* -ExcludeJobName dbatoolsci_testjob_disabled
87+
It "Should work with multiple wildcard passed in (see #9572)" {
88+
$results.Count | Should -Be 2
89+
}
8890
}
8991
}

0 commit comments

Comments
 (0)