Skip to content
Permalink
Browse files

Get-DbaAgentJob - Add ExcludeCategory parameter

  • Loading branch information
ben-thul committed Mar 24, 2020
1 parent 81e3704 commit 0e98eaf735a87e392228645ed65f9a912cae0685
Showing with 28 additions and 3 deletions.
  1. +8 −1 functions/Get-DbaAgentJob.ps1
  2. +20 −2 tests/Get-DbaAgentJob.Tests.ps1
@@ -31,6 +31,9 @@ function Get-DbaAgentJob {
.PARAMETER Category
Return jobs associated with specific category
.PARAMETER ExcludeCategory
Categories to exclude - jobs associated with these categories will not be returned.
.PARAMETER 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.
@@ -90,6 +93,7 @@ function Get-DbaAgentJob {
[string[]]$ExcludeJob,
[string[]]$Database,
[string[]]$Category,
[string[]]$ExcludeCategory,
[switch]$ExcludeDisabledJobs,
[switch]$EnableException
)
@@ -122,6 +126,9 @@ function Get-DbaAgentJob {
if ($Category) {
$jobs = $jobs | Where-Object Category -in $Category
}
if ($ExcludeCategory) {
$jobs = $jobs | Where-Object Category -notin $ExcludeCategory
}

foreach ($agentJob in $jobs) {
Add-Member -Force -InputObject $agentJob -MemberType NoteProperty -Name ComputerName -value $agentJob.Parent.Parent.ComputerName
@@ -132,4 +139,4 @@ function Get-DbaAgentJob {
}
}
}
}
}
@@ -5,7 +5,7 @@ Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan
Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Context "Validate parameters" {
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Job', 'ExcludeJob', 'Database', 'Category', 'ExcludeDisabledJobs', 'EnableException'
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Job', 'ExcludeJob', 'Database', 'Category', 'ExcludeDisabledJobs', 'EnableException', 'ExcludeCategory'
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
It "Should only contain our specific parameters" {
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
@@ -58,4 +58,22 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
$results.name -contains "dbatoolsci_testjob" | Should Be $False
}
}
}
Context "Command doesn't get excluded category" {
BeforeAll {
$null = New-DbaAgentJobCategory -SqlInstance $script:instance2 -Category 'Cat1'
$null = New-DbaAgentJobCategory -SqlInstance $script:instance2 -Category 'Cat2'

$null = New-DbaAgentJob -SqlInstance $script:instance2 -Job dbatoolsci_testjob_cat1 -Category 'Cat1'
$null = New-DbaAgentJob -SqlInstance $script:instance2 -Job dbatoolsci_testjob_cat2 -Category 'Cat2'
}
AfterAll {
$null = Remove-DbaAgentJobCategory -SqlInstance $script:instance2 -Category 'Cat1', 'Cat2'

$null = Remove-DbaAgentJob -SqlInstance $script:instance2 -Job dbatoolsci_testjob_cat1, dbatoolsci_testjob_cat2
}
$results = Get-DbaAgentJob -SqlInstance $script:instance2 -ExcludeCategory 'Cat2' | Where-Object {$_.Name -match "dbatoolsci"}
It "Should not return excluded job" {
$results.name -contains "dbatoolsci_testjob_cat2" | Should Be $False
}
}
}

0 comments on commit 0e98eaf

Please sign in to comment.
You can’t perform that action at this time.