Skip to content

Commit eead219

Browse files
Copy-DbaAgentJob - Fix regression where -Job parameter prevents copying all jobs (#9983)
Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com>
1 parent d4a62a5 commit eead219

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

public/Copy-DbaAgentJob.ps1

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,17 @@ function Copy-DbaAgentJob {
127127
begin {
128128
if ($Source) {
129129
try {
130-
$InputObject = Get-DbaAgentJob -SqlInstance $Source -SqlCredential $SourceSqlCredential -Job $Job -ExcludeJob $ExcludeJob
130+
$splatGetJob = @{
131+
SqlInstance = $Source
132+
SqlCredential = $SourceSqlCredential
133+
}
134+
if (Test-Bound 'Job') {
135+
$splatGetJob['Job'] = $Job
136+
}
137+
if (Test-Bound 'ExcludeJob') {
138+
$splatGetJob['ExcludeJob'] = $ExcludeJob
139+
}
140+
$InputObject = Get-DbaAgentJob @splatGetJob
131141
} catch {
132142
Stop-Function -Message "Error occurred while establishing connection to $Source" -Category ConnectionError -ErrorRecord $_ -Target $Source
133143
return
@@ -160,10 +170,14 @@ function Copy-DbaAgentJob {
160170
DateTime = [DbaDateTime](Get-Date)
161171
}
162172

163-
if ($Job -and $jobName -notin $Job -or $jobName -in $ExcludeJob) {
173+
if ((Test-Bound 'Job') -and $jobName -notin $Job) {
164174
Write-Message -Level Verbose -Message "Job [$jobName] filtered. Skipping."
165175
continue
166176
}
177+
if ((Test-Bound 'ExcludeJob') -and $jobName -in $ExcludeJob) {
178+
Write-Message -Level Verbose -Message "Job [$jobName] excluded. Skipping."
179+
continue
180+
}
167181
Write-Message -Message "Working on job: $jobName" -Level Verbose
168182
$sql = "
169183
SELECT sp.[name] AS MaintenancePlanName

public/Sync-DbaAvailabilityGroup.ps1

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,19 @@ function Sync-DbaAvailabilityGroup {
335335

336336
if ($Exclude -notcontains "AgentJob") {
337337
Write-ProgressHelper -Activity $activity -StepNumber ($stepCounter++) -Message "Syncing Agent Jobs"
338-
Copy-DbaAgentJob -Source $server -Destination $secondaries -Force:$force -Job $Job -ExcludeJob $ExcludeJob -DisableOnDestination:$DisableJobOnDestination
338+
$splatCopyJob = @{
339+
Source = $server
340+
Destination = $secondaries
341+
Force = $force
342+
DisableOnDestination = $DisableJobOnDestination
343+
}
344+
if (Test-Bound 'Job') {
345+
$splatCopyJob['Job'] = $Job
346+
}
347+
if (Test-Bound 'ExcludeJob') {
348+
$splatCopyJob['ExcludeJob'] = $ExcludeJob
349+
}
350+
Copy-DbaAgentJob @splatCopyJob
339351
}
340352

341353
if ($Exclude -notcontains "LoginPermissions") {

tests/Copy-DbaAgentJob.Tests.ps1

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,23 @@ Describe $CommandName -Tag IntegrationTests {
125125
}
126126
}
127127

128+
Context "Regression test for issue #9982" {
129+
It "copies all jobs when -Job parameter is not specified" {
130+
# Copy all jobs without specifying -Job parameter, using -Force to ensure they copy even if they exist
131+
$results = Copy-DbaAgentJob -Source $TestConfig.instance2 -Destination $TestConfig.instance3 -Force
132+
133+
# Both jobs should be copied
134+
$results.Name | Should -Contain "dbatoolsci_copyjob"
135+
$results.Name | Should -Contain "dbatoolsci_copyjob_disabled"
136+
$results.Status | Should -Not -Contain "Skipped"
137+
$results.Status | Should -Not -Contain "Failed"
138+
139+
# Verify jobs exist on destination
140+
$destJobsCopied = Get-DbaAgentJob -SqlInstance $TestConfig.instance3 -Job dbatoolsci_copyjob, dbatoolsci_copyjob_disabled
141+
$destJobsCopied.Count | Should -BeGreaterOrEqual 2
142+
}
143+
}
144+
128145
Context "UseLastModified parameter" {
129146
BeforeAll {
130147
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true

0 commit comments

Comments
 (0)