Skip to content

Commit 7fee4ba

Browse files
ValidJobOwner
1 parent a30f969 commit 7fee4ba

File tree

3 files changed

+39
-23
lines changed

3 files changed

+39
-23
lines changed

source/checks/Agentv5.Tests.ps1

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ BeforeDiscovery {
3232
}
3333
}
3434

35+
#TODO : Clean this up
3536
Write-PSFMessage -Message "Instances = $($InstancesToTest.Name)" -Level Verbose
37+
38+
Write-PSFMessage -Message "JobOwner = $($InstancesToTest.JobOwner)" -Level Verbose
39+
Write-PSFMessage -Message "JobOwner = $($InstancesToTest.JobOwner.JobName)" -Level Verbose
40+
3641
Set-PSFConfig -Module dbachecks -Name global.notcontactable -Value $NotContactable
3742

3843
# Get-DbcConfig is expensive so we call it once
@@ -105,15 +110,26 @@ Describe "Database Mail Profile" -Tag DatabaseMailProfile, Agent -ForEach $Insta
105110
}
106111

107112
Describe "Agent Mail Profile" -Tag AgentMailProfile, Agent -ForEach $InstancesToTest {
108-
$skipAgentMailProfile = ($__dbcconfig | Where-Object { $_.Name -eq 'skip.agent.agentmailprofile' }).Value
113+
$skipAgentMailProfile = ($__dbcconfig | Where-Object { $_.Name -eq 'skip.agent.mailprofile' }).Value
109114

110-
Context "esting SQL Agent Alert System database mail profile is set on <_.Name>" {
115+
Context "Testing SQL Agent Alert System database mail profile is set on <_.Name>" {
111116
It "The SQL Server Agent Alert System has the mail profile <_.AgentMailProfile.ExpectedAgentMailProfile> enabled as profile on <_.Name>." -Skip:$skipAgentMailProfile { #-ForEach ($PSItem.DatabaseMailProfile | Where-Object ExpectedDatabaseMailProfile -NE 'null') {
112117
$PSItem.AgentMailProfile.ActualAgentMailProfile | Should -Be $PSItem.AgentMailProfile.ExpectedAgentMailProfile -Because 'The SQL Agent Alert System needs an enabled database mail profile to send alert emails'
113118
}
114119
}
115120
}
116121

122+
Describe "Valid Job Owner" -Tag ValidJobOwner, Agent -ForEach $InstancesToTest {
123+
$skipAgentJobTargetOwner = ($__dbcconfig | Where-Object { $_.Name -eq 'skip.agent.jobowner' }).Value
124+
125+
Context "Testing SQL Agent Job Owner on <_.Name>" {
126+
It "The Job <_.JobName> has the Job Owner <_.ActualJobOwnerName> that should exist in this list ($([String]::Join(', ', "<_.ExpectedJobOwnerName>"))) on <_.InstanceName>" -Skip:$skipAgentJobTargetOwner -ForEach ($PSItem.JobOwner) {
127+
$PSItem.ActualJobOwnerName | Should -BeIn $PSItem.ExpectedJobOwnerName -Because 'The account that is the job owner is not what was expected'
128+
}
129+
}
130+
}
131+
132+
117133

118134

119135
# Describe "Failed Jobs" -Tags FailedJob, $filename {
@@ -151,26 +167,6 @@ Describe "Agent Mail Profile" -Tag AgentMailProfile, Agent -ForEach $InstancesTo
151167
# }
152168
# }
153169

154-
# Describe "Valid Job Owner" -Tags ValidJobOwner, $filename {
155-
# [string[]]$targetowner = Get-DbcConfigValue agent.validjobowner.name
156-
157-
# if ($NotContactable -contains $psitem) {
158-
# Context "Testing job owners on $psitem" {
159-
# It "Can't Connect to $Psitem" {
160-
# $false | Should -BeTrue -Because "The instance should be available to be connected to!"
161-
# }
162-
# }
163-
# }
164-
# else {
165-
# Context "Testing job owners on $psitem" {
166-
# @(Get-DbaAgentJob -SqlInstance $psitem -EnableException:$false).ForEach{
167-
# It "Job $($psitem.Name) - owner $($psitem.OwnerLoginName) should be in this list ( $( [String]::Join(", ", $targetowner) ) ) on $($psitem.SqlInstance)" {
168-
# $psitem.OwnerLoginName | Should -BeIn $TargetOwner -Because "The account that is the job owner is not what was expected"
169-
# }
170-
# }
171-
# }
172-
# }
173-
# }
174170
# Describe "Invalid Job Owner" -Tags InValidJobOwner, $filename {
175171
# [string[]]$targetowner = Get-DbcConfigValue agent.invalidjobowner.name
176172

source/internal/configurations/configuration.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ Set-PSFConfig -Module dbachecks -Name skip.agent.databasemailprofile -Validation
345345
Set-PSFConfig -Module dbachecks -Name skip.agent.mailprofile -Validation bool -Value $false -Initialize -Description "Skip the SQL Server Agent Mail Profile check"
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"
348+
Set-PSFConfig -Module dbachecks -Name skip.agent.jobowner -Validation bool -Value $false -Initialize -Description "Skip the Agent Job Owner check"
348349

349350

350351

source/internal/functions/Get-AllAgentInfo.ps1

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ function Get-AllAgentInfo {
3232
# JobServer Initial fields
3333
$AgentMailProfileInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Agent.JobServer])
3434

35-
35+
# Database Mail Profile Initial fields
3636
$DatabaseMailProfileInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Mail.MailProfile])
3737

38+
# JobOwner Initial fields
39+
$JobOwnerInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Agent.Job])
40+
3841
# Database Initial Fields
3942
$DatabaseInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Database])
4043

@@ -160,6 +163,7 @@ function Get-AllAgentInfo {
160163
$ConfigValues | Add-Member -MemberType NoteProperty -Name 'AgentMailProfile' -Value (Get-DbcConfigValue agent.databasemailprofile)
161164

162165
$agentMailProfile = $ConfigValues.AgentMailProfile.ForEach{
166+
163167
[PSCustomObject]@{
164168
InstanceName = $Instance.Name
165169
ExpectedAgentMailProfile = $ConfigValues.AgentMailProfile
@@ -185,7 +189,21 @@ function Get-AllAgentInfo {
185189

186190
}
187191
'ValidJobOwner' {
192+
$JobOwnerInitFields.Add("OwnerLoginName") | Out-Null # so we can check Job Owner
193+
$JobOwnerInitFields.Add("Name") | Out-Null # so we can check Job Name
194+
$Instance.SetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Agent.Job], $JobOwnerInitFields)
195+
$JobOwnerInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Agent.Job])
196+
197+
$ConfigValues | Add-Member -MemberType NoteProperty -Name 'TargetJobOwner' -Value (Get-DbcConfigValue agent.validjobowner.name)
188198

199+
$JobOwner = $Instance.JobServer.Jobs.ForEach{
200+
[PSCustomObject]@{
201+
InstanceName = $Instance.Name
202+
JobName = $PSItem.Name
203+
ExpectedJobOwnerName = $ConfigValues.TargetJobOwner #$PSItem
204+
ActualJobOwnerName = $PSItem.OwnerLoginName
205+
}
206+
}
189207
}
190208
'InValidJobOwner' {
191209

@@ -219,6 +237,7 @@ function Get-AllAgentInfo {
219237
FailSafeOperator = @($failsafeOperator)
220238
DatabaseMailProfile = @($databaseMailProfile)
221239
AgentMailProfile = @($agentMailProfile)
240+
JobOwner = $JobOwner
222241
}
223242
return $testInstanceObject
224243
}

0 commit comments

Comments
 (0)