Skip to content
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
18 changes: 16 additions & 2 deletions public/Copy-DbaAgentJob.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -160,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
Expand Down
14 changes: 13 additions & 1 deletion public/Sync-DbaAvailabilityGroup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
17 changes: 17 additions & 0 deletions tests/Copy-DbaAgentJob.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,23 @@ Describe $CommandName -Tag IntegrationTests {
}
}

Context "Regression test for issue #9982" {
It "copies all jobs when -Job parameter is not specified" {
# 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"
$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
Expand Down