Skip to content

Commit 35c4152

Browse files
InvalidJobOwner
1 parent 7fee4ba commit 35c4152

File tree

3 files changed

+38
-21
lines changed

3 files changed

+38
-21
lines changed

source/checks/Agentv5.Tests.ps1

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,23 @@ Describe "Valid Job Owner" -Tag ValidJobOwner, Agent -ForEach $InstancesToTest {
130130
}
131131

132132

133+
Describe "Invalid Job Owner" -Tag InvalidJobOwner, Agent -ForEach $InstancesToTest {
134+
$skipAgentJobTargetInvalidOwner = ($__dbcconfig | Where-Object { $_.Name -eq 'skip.agent.invalidjobowner.name' }).Value
135+
136+
Context "Testing Invalid SQL Agent Job Owner on <_.Name>" {
137+
It "The Job <_.JobName> has the Job Owner <_.ActualJobOwnerName> that shouldn't exist in this list ($([String]::Join(', ', "<_.InvalidJobOwnerName>"))) on <_.InstanceName>" -Skip:$skipAgentJobTargetOwner -ForEach ($PSItem.InvalidJobOwner) {
138+
$PSItem.ActualJobOwnerName | Should -Not -BeIn $PSItem.InvalidJobOwnerName -Because 'The account that is the job owner has been defined as not valid'
139+
}
140+
}
141+
}
142+
143+
144+
145+
146+
147+
148+
149+
133150

134151

135152
# Describe "Failed Jobs" -Tags FailedJob, $filename {
@@ -167,26 +184,6 @@ Describe "Valid Job Owner" -Tag ValidJobOwner, Agent -ForEach $InstancesToTest {
167184
# }
168185
# }
169186

170-
# Describe "Invalid Job Owner" -Tags InValidJobOwner, $filename {
171-
# [string[]]$targetowner = Get-DbcConfigValue agent.invalidjobowner.name
172-
173-
# if ($NotContactable -contains $psitem) {
174-
# Context "Testing job owners on $psitem" {
175-
# It "Can't Connect to $Psitem" {
176-
# $false | Should -BeTrue -Because "The instance should be available to be connected to!"
177-
# }
178-
# }
179-
# }
180-
# else {
181-
# Context "Testing job owners on $psitem" {
182-
# @(Get-DbaAgentJob -SqlInstance $psitem -EnableException:$false).ForEach{
183-
# It "Job $($psitem.Name) - owner $($psitem.OwnerLoginName) should not be in this list ( $( [String]::Join(", ", $targetowner) ) ) on $($psitem.SqlInstance)" {
184-
# $psitem.OwnerLoginName | Should -Not -BeIn $TargetOwner -Because "The account that is the job owner has been defined as not valid"
185-
# }
186-
# }
187-
# }
188-
# }
189-
# }
190187

191188
# Describe "Agent Alerts" -Tags AgentAlert, $filename {
192189
# $severity = Get-DbcConfigValue agent.alert.Severity

source/internal/configurations/configuration.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ Set-PSFConfig -Module dbachecks -Name skip.agent.mailprofile -Validation bool -V
346346
Set-PSFConfig -Module dbachecks -Name skip.agent.longrunningjobs -Validation bool -Value $false -Initialize -Description "Skip the long running agent jobs check"
347347
Set-PSFConfig -Module dbachecks -Name skip.agent.lastjobruntime -Validation bool -Value $false -Initialize -Description "Skip the last agent job time check"
348348
Set-PSFConfig -Module dbachecks -Name skip.agent.jobowner -Validation bool -Value $false -Initialize -Description "Skip the Agent Job Owner check"
349+
Set-PSFConfig -Module dbachecks -Name skip.agent.invalidjobowner.name -Validation bool -Value $false -Initialize -Description "Skip the Agent Job Invalid Owner check"
349350

350351

351352

source/internal/functions/Get-AllAgentInfo.ps1

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ function Get-AllAgentInfo {
3838
# JobOwner Initial fields
3939
$JobOwnerInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Agent.Job])
4040

41+
# Invalid JobOwner Initial fields
42+
$InvalidJobOwnerInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Agent.Job])
43+
4144
# Database Initial Fields
4245
$DatabaseInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Database])
4346

@@ -205,7 +208,22 @@ function Get-AllAgentInfo {
205208
}
206209
}
207210
}
208-
'InValidJobOwner' {
211+
'InvalidJobOwner' {
212+
$InvalidJobOwnerInitFields.Add("OwnerLoginName") | Out-Null # so we can check Job Owner
213+
$InvalidJobOwnerInitFields.Add("Name") | Out-Null # so we can check Job Name
214+
$Instance.SetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Agent.Job], $InvalidJobOwnerInitFields)
215+
$InvalidJobOwnerInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Agent.Job])
216+
217+
$ConfigValues | Add-Member -MemberType NoteProperty -Name 'InvalidJobOwner' -Value (Get-DbcConfigValue agent.invalidjobowner.name)
218+
219+
$InvalidJobOwner = $Instance.JobServer.Jobs.ForEach{
220+
[PSCustomObject]@{
221+
InstanceName = $Instance.Name
222+
JobName = $PSItem.Name
223+
ExpectedJobOwnerName = $ConfigValues.InvalidJobOwner
224+
ActualJobOwnerName = $PSItem.OwnerLoginName
225+
}
226+
}
209227

210228
}
211229
'AgentAlert' {
@@ -238,6 +256,7 @@ function Get-AllAgentInfo {
238256
DatabaseMailProfile = @($databaseMailProfile)
239257
AgentMailProfile = @($agentMailProfile)
240258
JobOwner = $JobOwner
259+
InvalidJobOwner = $InvalidJobOwner
241260
}
242261
return $testInstanceObject
243262
}

0 commit comments

Comments
 (0)