Skip to content

Commit a45b19b

Browse files
add SQL Agent Account (still working on #887)
1 parent 914b9de commit a45b19b

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

checks/Agentv5.Tests.ps1

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,40 @@ BeforeDiscovery {
3737

3838

3939
Describe "Database Mail XPs" -Tag DatabaseMailEnabled, CIS, security -ForEach $InstancesToTest {
40-
$DatabaseMailEnabled = Get-DbcConfigValue policy.security.DatabaseMailEnabled
4140
$skip = Get-DbcConfigValue skip.agent.databasemailenabled
4241
Context "Testing Database Mail XPs on <_.Name>" {
43-
It "Testing Database Mail XPs is set to $DatabaseMailEnabled on <_.Name>" -Skip:$skip {
44-
$PSItem.Configuration.DatabaseMailEnabled | Should -Be $PSItem.ConfigValues.DatabaseMailEnabled -Because 'The Database Mail XPs setting should be set correctly'
42+
It "Testing Database Mail XPs is set to <_.DatabaseMailEnabled> on <_.Name>" -Skip:$skip {
43+
$PSItem.DatabaseMailEnabled | Should -Be $PSItem.ConfigValues.DatabaseMailEnabled -Because 'The Database Mail XPs setting should be set correctly'
4544
}
4645
}
4746
}
4847

48+
Describe "SQL Agent Account" -Tag AgentServiceAccount, ServiceAccount -ForEach $InstancesToTest {
49+
#can't check agent on container - hmm does this actually work with instance need to check
50+
#if (-not $IsLinux -and ($PSItem.HostPlatform -ne 'Linux')) {
51+
$skipServiceState = Get-DbcConfigValue skip.agent.servicestate
52+
$skipServiceStartMode = Get-DbcConfigValue skip.agent.servicestartmode
53+
54+
Write-PSFMessage -Message "Agent = $($PSItem | Out-String)" -Level Verbose
55+
56+
Context "Testing SQL Agent is running on <_.Name>" {
57+
It "SQL Agent should be running for <_.InstanceName> on <_.Name>" -Skip:$skipServiceState {
58+
$PSItem.Agent.State | Should -Be "Running" -Because 'The agent service is required to run SQL Agent jobs'
59+
}
60+
}
61+
if ($PSItem.IsClustered) {
62+
It "SQL Agent service should have a start mode of Manual for FailOver Clustered Instance <_.InstanceName> on <_.Name>" -Skip:$skipServiceStartMode {
63+
$PSItem.Agent.StartMode | Should -Be "Manual" -Because 'Clustered Instances required that the Agent service is set to manual'
64+
}
65+
}
66+
else {
67+
It "SQL Agent service should have a start mode of Automatic for standalone instance <_.InstanceName> on <_.Name>" -Skip:$skipServiceStartMode {
68+
$PSItem.Agent.StartMode | Should -Be "Automatic" -Because 'Otherwise the Agent Jobs wont run if the server is restarted'
69+
}
70+
}
71+
#}
72+
}
73+
4974
# Describe "SQL Agent Account" -Tags AgentServiceAccount, ServiceAccount, $filename {
5075
# if ($NotContactable -contains $psitem) {
5176
# Context "Testing SQL Agent is running on $psitem" {
@@ -466,4 +491,7 @@ Describe "Database Mail XPs" -Tag DatabaseMailEnabled, CIS, security -ForEach $I
466491
# }
467492
# }
468493
# }
469-
# }
494+
# }
495+
496+
497+

internal/functions/Get-AllAgentInfo.ps1

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,22 @@ function Get-AllAgentInfo {
5454
$ConfigValues | Add-Member -MemberType NoteProperty -Name 'databasemailenabled' -Value (Get-DbcConfigValue policy.security.databasemailenabled)
5555
}
5656
'AgentServiceAccount' {
57-
57+
<#
58+
- IsLinux
59+
- HostPlatform
60+
- Agent.State
61+
- Agent.StartMode
62+
#>
63+
if (($Instance.VersionMajor -ge 14) -or $IsLinux -or $Instance.HostPlatform -eq 'Linux') {
64+
$Agent = @($Instance.Query("SELECT * FROM sys.dm_server_services") | Where-Object servicename -like '*Agent*').Foreach{
65+
[PSCustomObject]@{
66+
State = $PSItem.status_desc
67+
StartMode = $PSItem.startup_type_desc
68+
}
69+
}
70+
} else { # Windows
71+
$Agent = @(Get-DbaService -ComputerName $Instance.ComputerName -Type Agent)
72+
}
5873
}
5974
'DbaOperator' {
6075

@@ -97,7 +112,11 @@ function Get-AllAgentInfo {
97112
ComputerName = $Instance.ComputerName
98113
InstanceName = $Instance.DbaInstanceName
99114
Name = $Instance.Name
100-
DatabaseMailEnabled = $Instance.Configuration.DatabaseMailEnabled.RunValue
115+
ConfigValues = @($ConfigValues)
116+
HostPlatform = $Instance.HostPlatform
117+
IsClustered = $Instance.IsClustered
118+
DatabaseMailEnabled = $Instance.Configuration.DatabaseMailEnabled.ConfigValue
119+
Agent = @($Agent)
101120
}
102121
return $testInstanceObject
103122
}

0 commit comments

Comments
 (0)