Skip to content

Commit c1d2983

Browse files
add FailsafeOperator (but not working)
1 parent 140cbab commit c1d2983

File tree

3 files changed

+74
-31
lines changed

3 files changed

+74
-31
lines changed

source/checks/Agentv5.Tests.ps1

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ BeforeDiscovery {
1414
$Instance = $psitem
1515
try {
1616
$InstanceSMO = Connect-DbaInstance -SqlInstance $Instance -ErrorAction SilentlyContinue -ErrorVariable errorvar
17-
}
18-
catch {
17+
} catch {
1918
$NotContactable += $Instance
2019
}
2120
if ($NotContactable -notcontains $psitem) {
@@ -49,38 +48,56 @@ Describe "SQL Agent Account" -Tag AgentServiceAccount, ServiceAccount -ForEach $
4948
$skipServiceState = Get-DbcConfigValue skip.agent.servicestate
5049
$skipServiceStartMode = Get-DbcConfigValue skip.agent.servicestartmode
5150

52-
Context "Testing SQL Agent is running on <_.Name>" {
53-
It "SQL Agent should be running for <_.InstanceName> on <_.Name>" -Skip:$skipServiceState {
54-
$PSItem.Agent.State | Should -Be "Running" -Because 'The agent service is required to run SQL Agent jobs'
51+
# cant check agent on container - hmm does this actually work with instance need to check
52+
if (-not $IsLinux -and ($InstanceSMO.HostPlatform -ne 'Linux')) {
53+
Context "Testing SQL Agent is running on <_.Name>" {
54+
It "SQL Agent should be running for <_.InstanceName> on <_.Name>" -Skip:$skipServiceState {
55+
$PSItem.Agent.State | Should -Be "Running" -Because 'The agent service is required to run SQL Agent jobs'
56+
}
5557
}
56-
}
57-
if ($PSItem.IsClustered) {
58-
It "SQL Agent service should have a start mode of Manual for FailOver Clustered Instance <_.InstanceName> on <_.Name>" -Skip:$skipServiceStartMode {
59-
$PSItem.Agent.StartMode | Should -Be "Manual" -Because 'Clustered Instances required that the Agent service is set to manual'
58+
if ($PSItem.IsClustered) {
59+
It "SQL Agent service should have a start mode of Manual for FailOver Clustered Instance <_.InstanceName> on <_.Name>" -Skip:$skipServiceStartMode {
60+
$PSItem.Agent.StartMode | Should -Be "Manual" -Because 'Clustered Instances required that the Agent service is set to manual'
61+
}
62+
} else {
63+
It "SQL Agent service should have a start mode of Automatic for standalone instance <_.InstanceName> on <_.Name>" -Skip:$skipServiceStartMode {
64+
$PSItem.Agent.StartMode | Should -Be "Automatic" -Because 'Otherwise the Agent Jobs wont run if the server is restarted'
65+
}
6066
}
61-
}
62-
else {
63-
It "SQL Agent service should have a start mode of Automatic for standalone instance <_.InstanceName> on <_.Name>" -Skip:$skipServiceStartMode {
64-
$PSItem.Agent.StartMode | Should -Be "Automatic" -Because 'Otherwise the Agent Jobs wont run if the server is restarted'
67+
} else {
68+
Context "Testing SQL Agent is running on $psitem" {
69+
It "Running on Linux or connecting to container so can't check Services on $Psitem" -Skip {
70+
}
6571
}
6672
}
6773
}
6874

69-
Describe "DBA Operators" -Tag DbaOperator, Operator -ForEach $InstancesToTest {
75+
Describe "DBA Operator" -Tag DbaOperator, Operator -ForEach $InstancesToTest {
7076
$skipOperatorName = Get-DbcConfigValue skip.agent.operatorname
71-
$skipOperatorEamil = Get-DbcConfigValue skip.agent.operatoremail
77+
$skipOperatorEmail = Get-DbcConfigValue skip.agent.operatoremail
7278

7379
Context "Testing DBA Operators exists on <_.Name>" {
74-
It "The Operator <_.ExpectedOperatorName> exists on <_.Name>" -Skip:$skipOperatorName -ForEach ($PSItem.Operator | Where-Object ExpectedOperatorName -ne 'null') {
80+
It "The Operator <_.ExpectedOperatorName> exists on <_.Name>" -Skip:$skipOperatorName -ForEach ($PSItem.Operator | Where-Object ExpectedOperatorName -NE 'null') {
7581
$PSItem.ExpectedOperatorName | Should -BeIn $PSItem.ActualOperatorName -Because 'This Operator is expected to exist'
7682
}
7783

78-
It "The Operator email <_.ExpectedOperatorEmail> is correct on <_.Name>" -Skip:$skipOperatorEamil -ForEach ($PSItem.Operator | Where-Object ExpectedOperatorEmail -ne 'null') {
84+
It "The Operator email <_.ExpectedOperatorEmail> is correct on <_.Name>" -Skip:$skipOperatorEmail -ForEach ($PSItem.Operator | Where-Object ExpectedOperatorEmail -NE 'null') {
7985
$PSItem.ExpectedOperatorEmail | Should -BeIn $PSItem.ActualOperatorEmail -Because 'This operator email is expected to exist'
8086
}
8187
}
8288
}
8389

90+
Describe "Failsafe Operator" -Tag FailsafeOperator, Operator -ForEach $InstancesToTest {
91+
$skipFailsafeoperator = Get-DbcConfigValue skip.agent.failsafeoperator
92+
$failsafeoperator = Get-DbcConfigValue agent.failsafeoperator
93+
94+
Context "Testing failsafe Operators exists on <_.Name>" {
95+
It "The Failsafe operator <_.FailSafeOperator> exists on <_.Name>" -Skip:$skipFailsafeoperator -ForEach ($PSItem.JobServer.AlertSystem | Where-Object FailSafeOperator -NE $null) {
96+
$PSItem.ExpectedFailSafeOperator | Should -Be $failsafeoperator -Because 'The failsafe operator will ensure that any job failures will be notified to someone if not set explicitly'
97+
}
98+
}
99+
}
100+
84101
# Describe "Failsafe Operator" -Tags FailsafeOperator, Operator, $filename {
85102
# if ($NotContactable -contains $psitem) {
86103
# Context "Testing failsafe operator exists on $psitem" {

source/internal/configurations/configuration.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ Set-PSFConfig -Module dbachecks -Name skip.agent.servicestartmode -Validation bo
334334
Set-PSFConfig -Module dbachecks -Name skip.agent.servicestate -Validation bool -Value $false -Initialize -Description "Skip the Agent Service Start Mode check"
335335
Set-PSFConfig -Module dbachecks -Name skip.agent.operatorname -Validation bool -Value $false -Initialize -Description "Skip the Agent Operator Name check"
336336
Set-PSFConfig -Module dbachecks -Name skip.agent.operatoremail -Validation bool -Value $false -Initialize -Description "Skip the Agent Operator Email check"
337+
Set-PSFConfig -Module dbachecks -Name skip.agent.failsafeoperator -Validation bool -Value $false -Initialize -Description "Skip the Agent Failsafe Operator check"
337338
Set-PSFConfig -Module dbachecks -Name skip.agent.longrunningjobs -Validation bool -Value $false -Initialize -Description "Skip the long running agent jobs check"
338339
Set-PSFConfig -Module dbachecks -Name skip.agent.lastjobruntime -Validation bool -Value $false -Initialize -Description "Skip the last agent job time check"
339340

@@ -367,7 +368,7 @@ Set-PSFConfig -Module dbachecks -Name skip.security.serverprotocol -Validation b
367368
#agent
368369
Set-PSFConfig -Module dbachecks -Name agent.dbaoperatorname -Value $null -Initialize -Description "Name of the DBA Operator in SQL Agent"
369370
Set-PSFConfig -Module dbachecks -Name agent.dbaoperatoremail -Value $null -Initialize -Description "Email address of the DBA Operator in SQL Agent"
370-
Set-PSFConfig -Module dbachecks -Name agent.failsafeoperator -Value $null -Initialize -Description "Email address of the DBA Operator in SQL Agent"
371+
Set-PSFConfig -Module dbachecks -Name agent.failsafeoperator -Value $null -Initialize -Description "Email address of the Failsafe Operator in SQL Agent"
371372
Set-PSFConfig -Module dbachecks -Name agent.databasemailprofile -Value $null -Initialize -Description "Name of the Database Mail Profile in SQL Agent"
372373
Set-PSFConfig -Module dbachecks -Name agent.validjobowner.name -Value "sa" -Initialize -Description "Agent job owner account should be this user"
373374
Set-PSFConfig -Module dbachecks -Name agent.invalidjobowner.name -Value $null -Initialize -Description "Agent job owner account should not be this user"

source/internal/functions/Get-AllAgentInfo.ps1

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ function Get-AllAgentInfo {
2424
# Job Server Initial fields
2525
$OperatorInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Agent.Operator])
2626

27+
# Job Server Alert System Initial fields
28+
$FailsafeInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Agent.AlertSystem])
29+
2730
# Database Initial Fields
2831
$DatabaseInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Database])
2932

@@ -59,13 +62,14 @@ function Get-AllAgentInfo {
5962
}
6063
'AgentServiceAccount' {
6164
if (($Instance.VersionMajor -ge 14) -or $IsLinux -or $Instance.HostPlatform -eq 'Linux') {
62-
$Agent = @($Instance.Query("SELECT status_desc, startup_type_desc FROM sys.dm_server_services") | Where-Object servicename -like '*Agent*').ForEach{
65+
$Agent = @($Instance.Query("SELECT status_desc, startup_type_desc FROM sys.dm_server_services") | Where-Object servicename -Like '*Agent*').ForEach{
6366
[PSCustomObject]@{
64-
State = $PSItem.status_desc
67+
State = $PSItem.status_desc
6568
StartMode = $PSItem.startup_type_desc
6669
}
6770
}
68-
} else { # Windows
71+
} else {
72+
# Windows
6973
$Agent = @(Get-DbaService -ComputerName $Instance.ComputerName -Type Agent)
7074
}
7175
}
@@ -79,25 +83,46 @@ function Get-AllAgentInfo {
7983

8084
$Operator = $ConfigValues.DbaOperatorName.ForEach{
8185
[PSCustomObject]@{
82-
InstanceName = $Instance.Name
83-
ExpectedOperatorName = $PSItem
84-
ActualOperatorName = $Instance.JobServer.Operators.Name
85-
ExpectedOperatorEmail = 'null'
86-
ActualOperatorEmail = 'null'
86+
InstanceName = $Instance.Name
87+
ExpectedOperatorName = $PSItem
88+
ActualOperatorName = $Instance.JobServer.Operators.Name
89+
ExpectedOperatorEmail = 'null'
90+
ActualOperatorEmail = 'null'
8791
}
8892
}
8993

9094
$Operator += $ConfigValues.DbaOperatorEmail.ForEach{
9195
[PSCustomObject]@{
92-
InstanceName = $Instance.Name
93-
ExpectedOperatorName = 'null'
94-
ActualOperatorName = 'null'
95-
ExpectedOperatorEmail = $PSItem
96-
ActualOperatorEmail = $Instance.JobServer.Operators.EmailAddress
96+
InstanceName = $Instance.Name
97+
ExpectedOperatorName = 'null'
98+
ActualOperatorName = 'null'
99+
ExpectedOperatorEmail = $PSItem
100+
ActualOperatorEmail = $Instance.JobServer.Operators.EmailAddress
97101
}
98102
}
99103
}
100104
'FailsafeOperator' {
105+
$FailsafeInitFields.Add("Name") | Out-Null # so we can check failsafe operators
106+
$Instance.SetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Agent.AlertSystem], $FailsafeInitFields)
107+
$FailsafeInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Agent.AlertSystem])
108+
109+
$ConfigValues | Add-Member -MemberType NoteProperty -Name 'FailsafeOperator' -Value (Get-DbcConfigValue agent.failsafeoperator)
110+
111+
$Operator = $ConfigValues.FailsafeOperator.ForEach{
112+
[PSCustomObject]@{
113+
InstanceName = $Instance.Name
114+
ExpectedFailSafeOperator = $PSItem
115+
ActualOperatorName = $Instance.JobServer.AlertSystem.Name
116+
}
117+
}
118+
119+
$Operator += $ConfigValues.FailsafeOperator.ForEach{
120+
[PSCustomObject]@{
121+
InstanceName = $Instance.Name
122+
ExpectedFailSafeOperator = 'null'
123+
ActualOperatorName = 'null'
124+
}
125+
}
101126

102127
}
103128
'DatabaseMailProfile' {

0 commit comments

Comments
 (0)