Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find-DbaAgentJob, work with multiple wildcards #9583

Merged
merged 3 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 15 additions & 30 deletions private/functions/Get-JobList.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -69,59 +69,44 @@ function Get-JobList {

$jobs = $server.JobServer.Jobs
if ( (Test-Bound 'JobFilter') -or (Test-Bound 'StepFilter') ) {
if ($JobFilter.Count -gt 1) {
if ($Not) {
$jobs | Where-Object Name -NotIn $JobFilter
} else {
$jobs | Where-Object Name -In $JobFilter
}
} else {
foreach ($job in $jobs) {
if ($JobFilter -match '`*') {

foreach ($job in $jobs) {
foreach ($jFilter in $JobFilter) {
if ($jFilter -match '`*') {
if ($Not) {
$job | Where-Object Name -NotLike $JobFilter
$job | Where-Object Name -NotLike $jFilter
} else {
$job | Where-Object Name -Like $JobFilter
$job | Where-Object Name -Like $jFilter
}
} else {
if ($Not) {
$job | Where-Object Name -NE $JobFilter
$job | Where-Object Name -NE $jFilter
} else {
$job | Where-Object Name -EQ $JobFilter
$job | Where-Object Name -EQ $jFilter
}
}
if ($StepFilter -match '`*') {
if ($Not) {
$stepFound = $job.JobSteps | Where-Object Name -NotLike $StepFilter
if ($stepFound.Count -gt 0) {
$job
}
} else {
$stepFound = $job.JobSteps | Where-Object Name -Like $StepFilter
if ($stepFound.Count -gt 0) {
$job
}
}
} elseif ($StepName.Count -gt 1) {
}
foreach ($sFilter in $StepFilter) {
if ($sFilter -match '`*') {
if ($Not) {
$stepFound = $job.JobSteps | Where-Object Name -NotIn $StepName
$stepFound = $job.JobSteps | Where-Object Name -NotLike $sFilter
if ($stepFound.Count -gt 0) {
$job
}
} else {
$stepFound = $job.JobSteps | Where-Object Name -In $StepName
$stepFound = $job.JobSteps | Where-Object Name -Like $sFilter
if ($stepFound.Count -gt 0) {
$job
}
}
} else {
if ($Not) {
$stepFound = $job.JobSteps | Where-Object Name -NE $StepName
$stepFound = $job.JobSteps | Where-Object Name -NE $sFilter
if ($stepFound.Count -gt 0) {
$job
}
} else {
$stepFound = $job.JobSteps | Where-Object Name -EQ $StepName
$stepFound = $job.JobSteps | Where-Object Name -EQ $sFilter
if ($stepFound.Count -gt 0) {
$job
}
Expand Down
6 changes: 3 additions & 3 deletions public/Find-DbaAgentJob.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function Find-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
PS C:\> Find-DbaAgentJob -SqlInstance Dev01 -LastUsed 10 -Exclude "Yearly - RollUp Workload", "SMS - Notification"
PS C:\> Find-DbaAgentJob -SqlInstance Dev01 -LastUsed 10 -ExcludeJobName "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"

Expand Down Expand Up @@ -222,9 +222,9 @@ function Find-DbaAgentJob {
}
}

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

if ($Since) {
Expand Down
14 changes: 8 additions & 6 deletions tests/Find-DbaAgentJob.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
$null = New-DbaAgentJob -SqlInstance $TestConfig.instance2 -Job 'dbatoolsci_testjob' -OwnerLogin 'sa'
$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
$null = Start-DbaAgentJob -SqlInstance $TestConfig.instance2 -Job 'dbatoolsci_testjob'
$null = New-DbaAgentJob -SqlInstance $TestConfig.instance2 -Job 'dbatoolsci_testjob' -OwnerLogin 'sa'
$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
$null = New-DbaAgentJob -SqlInstance $TestConfig.instance2 -Job 'dbatoolsregr_testjob' -OwnerLogin 'sa'
$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
$null = New-DbaAgentJobCategory -SqlInstance $TestConfig.instance2 -Category 'dbatoolsci_job_category' -CategoryType LocalJob
$null = New-DbaAgentJob -SqlInstance $TestConfig.instance2 -Job 'dbatoolsci_testjob_disabled' -Category 'dbatoolsci_job_category' -Disabled
$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
}
AfterAll {
$null = Remove-DbaAgentJob -SqlInstance $TestConfig.instance2 -Job dbatoolsci_testjob, dbatoolsci_testjob_disabled -Confirm:$false
$null = Remove-DbaAgentJob -SqlInstance $TestConfig.instance2 -Job dbatoolsci_testjob, dbatoolsregr_testjob, dbatoolsci_testjob_disabled -Confirm:$false
$null = Remove-DbaAgentJobCategory -SqlInstance $TestConfig.instance2 -Category 'dbatoolsci_job_category' -Confirm:$false
}

$results = Find-DbaAgentJob -SqlInstance $TestConfig.instance2 -Job dbatoolsci_testjob
It "Should find a specific job" {
$results.name | Should Be "dbatoolsci_testjob"
}
$results = Find-DbaAgentJob -SqlInstance $TestConfig.instance2 -Job *dbatoolsci* -Exclude dbatoolsci_testjob_disabled
$results = Find-DbaAgentJob -SqlInstance $TestConfig.instance2 -Job *dbatoolsci* -ExcludeJobName dbatoolsci_testjob_disabled
It "Should find a specific job but not an excluded job" {
$results.name | Should Not Be "dbatoolsci_testjob_disabled"
}
Expand Down Expand Up @@ -79,11 +79,13 @@ Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
It "Should find jobs that are owned by sa" {
$results | Should not be null
}


$results = Find-DbaAgentJob -SqlInstance $TestConfig.instance2 -IsFailed -Since '2016-07-01 10:47:00'
It "Should find jobs that have been failed since July of 2016" {
$results | Should not be null
}
$results = Find-DbaAgentJob -SqlInstance $TestConfig.instance2 -Job *dbatoolsci*,*dbatoolsregr* -ExcludeJobName dbatoolsci_testjob_disabled
It "Should work with multiple wildcard passed in (see #9572)" {
$results.Count | Should -Be 2
}
}
}