Skip to content

Commit 676beb3

Browse files
Get-DbaAgentJob, Remove-DbaAgentJob - Add validation for null/empty Job parameter (#9931)
1 parent a5efeab commit 676beb3

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

public/Get-DbaAgentJob.ps1

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,22 @@ function Get-DbaAgentJob {
140140
$jobExecutionResults = $server.Query($query)
141141
}
142142

143+
# Check if Job parameter is bound with null, empty, or whitespace-only values
144+
if (Test-Bound 'Job') {
145+
if ($null -eq $Job -or $Job.Count -eq 0 -or ($Job | Where-Object { [string]::IsNullOrWhiteSpace($_) })) {
146+
Write-Message -Level Verbose -Message "The -Job parameter was explicitly provided but contains null, empty, or whitespace-only values. This may indicate an uninitialized variable. Skipping instance."
147+
continue
148+
}
149+
}
150+
151+
# Check if ExcludeJob parameter is bound with null, empty, or whitespace-only values
152+
if (Test-Bound 'ExcludeJob') {
153+
if ($null -eq $ExcludeJob -or $ExcludeJob.Count -eq 0 -or ($ExcludeJob | Where-Object { [string]::IsNullOrWhiteSpace($_) })) {
154+
Write-Message -Level Verbose -Message "The -ExcludeJob parameter was explicitly provided but contains null, empty, or whitespace-only values. This may indicate an uninitialized variable. Parameter will be ignored."
155+
$ExcludeJob = $null
156+
}
157+
}
158+
143159
$jobs = $server.JobServer.Jobs | Where-Object JobType -in $Type
144160

145161
if ($Job) {

public/Remove-DbaAgentJob.ps1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ function Remove-DbaAgentJob {
8787
[switch]$EnableException
8888
)
8989
process {
90+
# Check if Job parameter is bound with null, empty, or whitespace-only values
91+
if (Test-Bound 'Job') {
92+
if ($null -eq $Job -or $Job.Count -eq 0 -or ($Job | Where-Object { [string]::IsNullOrWhiteSpace($_) })) {
93+
Write-Message -Level Verbose -Message "The -Job parameter was explicitly provided but contains null, empty, or whitespace-only values. This may indicate an uninitialized variable. Skipping operation."
94+
return
95+
}
96+
}
97+
9098
foreach ($instance in $SqlInstance) {
9199
try {
92100
$server = Connect-DbaInstance -SqlInstance $instance -SqlCredential $SqlCredential

tests/Get-DbaAgentJob.Tests.ps1

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,27 @@ Describe $CommandName -Tag IntegrationTests {
133133
$resultMultipleDatabases.Name -contains $jobName2 | Should -BeTrue
134134
}
135135
}
136+
Context "Command validates null/empty Job parameter" {
137+
It "Should return no jobs when -Job is null" {
138+
$nullVariable = $null
139+
$results = Get-DbaAgentJob -SqlInstance $TestConfig.instance2 -Job $nullVariable
140+
$results | Should -BeNullOrEmpty
141+
}
142+
143+
It "Should return no jobs when -Job is empty string" {
144+
$results = Get-DbaAgentJob -SqlInstance $TestConfig.instance2 -Job ""
145+
$results | Should -BeNullOrEmpty
146+
}
147+
148+
It "Should return no jobs when -Job is whitespace" {
149+
$results = Get-DbaAgentJob -SqlInstance $TestConfig.instance2 -Job " "
150+
$results | Should -BeNullOrEmpty
151+
}
152+
153+
It "Should ignore -ExcludeJob when it contains null values" {
154+
$nullVariable = $null
155+
$results = Get-DbaAgentJob -SqlInstance $TestConfig.instance2 -ExcludeJob $nullVariable
156+
$results | Should -Not -BeNullOrEmpty
157+
}
158+
}
136159
}

tests/Remove-DbaAgentJob.Tests.ps1

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,39 @@ Describe $CommandName -Tag IntegrationTests {
123123
($server.Query("select 1 from sysjobhistory where job_id = '$jobId'", "msdb")) | Should -Not -BeNullOrEmpty
124124
}
125125
}
126+
127+
Context "Command validates null/empty Job parameter" {
128+
BeforeAll {
129+
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true
130+
$null = New-DbaAgentJob -SqlInstance $TestConfig.instance3 -Job dbatoolsci_testjob_validation
131+
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
132+
}
133+
134+
AfterAll {
135+
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true
136+
if (Get-DbaAgentJob -SqlInstance $TestConfig.instance3 -Job dbatoolsci_testjob_validation) {
137+
$null = Remove-DbaAgentJob -SqlInstance $TestConfig.instance3 -Job dbatoolsci_testjob_validation -Confirm:$false
138+
}
139+
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
140+
}
141+
142+
It "Should not remove jobs when -Job is null" {
143+
$nullVariable = $null
144+
$result = Remove-DbaAgentJob -SqlInstance $TestConfig.instance3 -Job $nullVariable -Confirm:$false
145+
$result | Should -BeNullOrEmpty
146+
(Get-DbaAgentJob -SqlInstance $TestConfig.instance3 -Job dbatoolsci_testjob_validation) | Should -Not -BeNullOrEmpty
147+
}
148+
149+
It "Should not remove jobs when -Job is empty string" {
150+
$result = Remove-DbaAgentJob -SqlInstance $TestConfig.instance3 -Job "" -Confirm:$false
151+
$result | Should -BeNullOrEmpty
152+
(Get-DbaAgentJob -SqlInstance $TestConfig.instance3 -Job dbatoolsci_testjob_validation) | Should -Not -BeNullOrEmpty
153+
}
154+
155+
It "Should not remove jobs when -Job is whitespace" {
156+
$result = Remove-DbaAgentJob -SqlInstance $TestConfig.instance3 -Job " " -Confirm:$false
157+
$result | Should -BeNullOrEmpty
158+
(Get-DbaAgentJob -SqlInstance $TestConfig.instance3 -Job dbatoolsci_testjob_validation) | Should -Not -BeNullOrEmpty
159+
}
160+
}
126161
} # $TestConfig.instance2 for appveyor

0 commit comments

Comments
 (0)