From 8d9237c47a09bd4ff023eb8845f96a291d89d846 Mon Sep 17 00:00:00 2001 From: "claude[bot]" <41898282+claude[bot]@users.noreply.github.com> Date: Thu, 20 Nov 2025 14:21:16 +0000 Subject: [PATCH 1/3] Copy-DbaAgentJob - Fix regression where -Job parameter prevents copying all jobs This fixes issue #9982 where Copy-DbaAgentJob no longer copies all jobs when the -Job parameter is not specified. The issue was introduced in PR #9931 when Get-DbaAgentJob added validation to reject null/empty values for -Job parameter. Changes: - Copy-DbaAgentJob: Use Test-Bound to only pass -Job/-ExcludeJob when explicitly provided - Sync-DbaAvailabilityGroup: Apply same fix for consistency - Add regression test to ensure all jobs are copied without -Job parameter (do Copy-DbaAgentJob) Co-authored-by: Chrissy LeMaire --- public/Copy-DbaAgentJob.ps1 | 12 +++++++++++- public/Sync-DbaAvailabilityGroup.ps1 | 14 +++++++++++++- tests/Copy-DbaAgentJob.Tests.ps1 | 20 ++++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/public/Copy-DbaAgentJob.ps1 b/public/Copy-DbaAgentJob.ps1 index 0f46f989f1f0..a845ab06c6da 100644 --- a/public/Copy-DbaAgentJob.ps1 +++ b/public/Copy-DbaAgentJob.ps1 @@ -127,7 +127,17 @@ function Copy-DbaAgentJob { begin { if ($Source) { try { - $InputObject = Get-DbaAgentJob -SqlInstance $Source -SqlCredential $SourceSqlCredential -Job $Job -ExcludeJob $ExcludeJob + $splatGetJob = @{ + SqlInstance = $Source + SqlCredential = $SourceSqlCredential + } + if (Test-Bound 'Job') { + $splatGetJob['Job'] = $Job + } + if (Test-Bound 'ExcludeJob') { + $splatGetJob['ExcludeJob'] = $ExcludeJob + } + $InputObject = Get-DbaAgentJob @splatGetJob } catch { Stop-Function -Message "Error occurred while establishing connection to $Source" -Category ConnectionError -ErrorRecord $_ -Target $Source return diff --git a/public/Sync-DbaAvailabilityGroup.ps1 b/public/Sync-DbaAvailabilityGroup.ps1 index 1b883bf81bb2..69f95e8fa50d 100644 --- a/public/Sync-DbaAvailabilityGroup.ps1 +++ b/public/Sync-DbaAvailabilityGroup.ps1 @@ -335,7 +335,19 @@ function Sync-DbaAvailabilityGroup { if ($Exclude -notcontains "AgentJob") { Write-ProgressHelper -Activity $activity -StepNumber ($stepCounter++) -Message "Syncing Agent Jobs" - Copy-DbaAgentJob -Source $server -Destination $secondaries -Force:$force -Job $Job -ExcludeJob $ExcludeJob -DisableOnDestination:$DisableJobOnDestination + $splatCopyJob = @{ + Source = $server + Destination = $secondaries + Force = $force + DisableOnDestination = $DisableJobOnDestination + } + if (Test-Bound 'Job') { + $splatCopyJob['Job'] = $Job + } + if (Test-Bound 'ExcludeJob') { + $splatCopyJob['ExcludeJob'] = $ExcludeJob + } + Copy-DbaAgentJob @splatCopyJob } if ($Exclude -notcontains "LoginPermissions") { diff --git a/tests/Copy-DbaAgentJob.Tests.ps1 b/tests/Copy-DbaAgentJob.Tests.ps1 index abfb80374997..12217bc332b4 100644 --- a/tests/Copy-DbaAgentJob.Tests.ps1 +++ b/tests/Copy-DbaAgentJob.Tests.ps1 @@ -125,6 +125,26 @@ Describe $CommandName -Tag IntegrationTests { } } + Context "Regression test for issue #9982" { + It "copies all jobs when -Job parameter is not specified" { + # Clean up any existing jobs from previous test runs + $null = Remove-DbaAgentJob -SqlInstance $TestConfig.instance3 -Job dbatoolsci_copyjob, dbatoolsci_copyjob_disabled -ErrorAction SilentlyContinue + + # Copy all jobs without specifying -Job parameter + $results = Copy-DbaAgentJob -Source $TestConfig.instance2 -Destination $TestConfig.instance3 + + # Both jobs should be copied + $results.Name | Should -Contain "dbatoolsci_copyjob" + $results.Name | Should -Contain "dbatoolsci_copyjob_disabled" + $results.Status | Should -Not -Contain "Skipped" + $results.Status | Should -Not -Contain "Failed" + + # Verify jobs exist on destination + $destJobsCopied = Get-DbaAgentJob -SqlInstance $TestConfig.instance3 -Job dbatoolsci_copyjob, dbatoolsci_copyjob_disabled + $destJobsCopied.Count | Should -BeGreaterOrEqual 2 + } + } + Context "UseLastModified parameter" { BeforeAll { $PSDefaultParameterValues["*-Dba*:EnableException"] = $true From 123b831b0dee5379db1f09c96ed7f83aa46d6b61 Mon Sep 17 00:00:00 2001 From: "claude[bot]" <41898282+claude[bot]@users.noreply.github.com> Date: Thu, 20 Nov 2025 19:23:23 +0000 Subject: [PATCH 2/3] Copy-DbaAgentJob - Fix process block filtering to use Test-Bound The filtering logic in the process block was checking $Job directly instead of using Test-Bound, which caused issues when the parameter wasn't explicitly provided. Split the condition into two separate checks for clarity. (do Copy-DbaAgentJob) Co-authored-by: Chrissy LeMaire --- public/Copy-DbaAgentJob.ps1 | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/public/Copy-DbaAgentJob.ps1 b/public/Copy-DbaAgentJob.ps1 index a845ab06c6da..716106c87ff2 100644 --- a/public/Copy-DbaAgentJob.ps1 +++ b/public/Copy-DbaAgentJob.ps1 @@ -170,10 +170,14 @@ function Copy-DbaAgentJob { DateTime = [DbaDateTime](Get-Date) } - if ($Job -and $jobName -notin $Job -or $jobName -in $ExcludeJob) { + if ((Test-Bound 'Job') -and $jobName -notin $Job) { Write-Message -Level Verbose -Message "Job [$jobName] filtered. Skipping." continue } + if ((Test-Bound 'ExcludeJob') -and $jobName -in $ExcludeJob) { + Write-Message -Level Verbose -Message "Job [$jobName] excluded. Skipping." + continue + } Write-Message -Message "Working on job: $jobName" -Level Verbose $sql = " SELECT sp.[name] AS MaintenancePlanName From 1668824c6b4a80f85d48b4413de398785e9a5665 Mon Sep 17 00:00:00 2001 From: "claude[bot]" <41898282+claude[bot]@users.noreply.github.com> Date: Sun, 23 Nov 2025 01:18:23 +0000 Subject: [PATCH 3/3] Copy-DbaAgentJob - Fix AppVeyor test failure by adding -Force to regression test The regression test was failing on AppVeyor because jobs from the previous test context already existed on the destination, causing them to be skipped without -Force. Adding -Force ensures all jobs are copied regardless of whether they already exist, which is what we want to test - that all jobs are processed when -Job parameter is not specified. (do Copy-DbaAgentJob) Co-authored-by: Chrissy LeMaire --- tests/Copy-DbaAgentJob.Tests.ps1 | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tests/Copy-DbaAgentJob.Tests.ps1 b/tests/Copy-DbaAgentJob.Tests.ps1 index 12217bc332b4..4f2054b32cb7 100644 --- a/tests/Copy-DbaAgentJob.Tests.ps1 +++ b/tests/Copy-DbaAgentJob.Tests.ps1 @@ -127,11 +127,8 @@ Describe $CommandName -Tag IntegrationTests { Context "Regression test for issue #9982" { It "copies all jobs when -Job parameter is not specified" { - # Clean up any existing jobs from previous test runs - $null = Remove-DbaAgentJob -SqlInstance $TestConfig.instance3 -Job dbatoolsci_copyjob, dbatoolsci_copyjob_disabled -ErrorAction SilentlyContinue - - # Copy all jobs without specifying -Job parameter - $results = Copy-DbaAgentJob -Source $TestConfig.instance2 -Destination $TestConfig.instance3 + # Copy all jobs without specifying -Job parameter, using -Force to ensure they copy even if they exist + $results = Copy-DbaAgentJob -Source $TestConfig.instance2 -Destination $TestConfig.instance3 -Force # Both jobs should be copied $results.Name | Should -Contain "dbatoolsci_copyjob"