Skip to content

Commit

Permalink
add SQL Agent Account (still working on #887)
Browse files Browse the repository at this point in the history
  • Loading branch information
ClaudioESSilva committed Apr 29, 2022
1 parent 914b9de commit a45b19b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
36 changes: 32 additions & 4 deletions checks/Agentv5.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,40 @@ BeforeDiscovery {


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

Describe "SQL Agent Account" -Tag AgentServiceAccount, ServiceAccount -ForEach $InstancesToTest {
#can't check agent on container - hmm does this actually work with instance need to check
#if (-not $IsLinux -and ($PSItem.HostPlatform -ne 'Linux')) {
$skipServiceState = Get-DbcConfigValue skip.agent.servicestate
$skipServiceStartMode = Get-DbcConfigValue skip.agent.servicestartmode

Write-PSFMessage -Message "Agent = $($PSItem | Out-String)" -Level Verbose

Context "Testing SQL Agent is running on <_.Name>" {
It "SQL Agent should be running for <_.InstanceName> on <_.Name>" -Skip:$skipServiceState {
$PSItem.Agent.State | Should -Be "Running" -Because 'The agent service is required to run SQL Agent jobs'
}
}
if ($PSItem.IsClustered) {
It "SQL Agent service should have a start mode of Manual for FailOver Clustered Instance <_.InstanceName> on <_.Name>" -Skip:$skipServiceStartMode {
$PSItem.Agent.StartMode | Should -Be "Manual" -Because 'Clustered Instances required that the Agent service is set to manual'
}
}
else {
It "SQL Agent service should have a start mode of Automatic for standalone instance <_.InstanceName> on <_.Name>" -Skip:$skipServiceStartMode {
$PSItem.Agent.StartMode | Should -Be "Automatic" -Because 'Otherwise the Agent Jobs wont run if the server is restarted'
}
}
#}
}

# Describe "SQL Agent Account" -Tags AgentServiceAccount, ServiceAccount, $filename {
# if ($NotContactable -contains $psitem) {
# Context "Testing SQL Agent is running on $psitem" {
Expand Down Expand Up @@ -466,4 +491,7 @@ Describe "Database Mail XPs" -Tag DatabaseMailEnabled, CIS, security -ForEach $I
# }
# }
# }
# }
# }



23 changes: 21 additions & 2 deletions internal/functions/Get-AllAgentInfo.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,22 @@ function Get-AllAgentInfo {
$ConfigValues | Add-Member -MemberType NoteProperty -Name 'databasemailenabled' -Value (Get-DbcConfigValue policy.security.databasemailenabled)
}
'AgentServiceAccount' {

<#
- IsLinux
- HostPlatform
- Agent.State
- Agent.StartMode
#>
if (($Instance.VersionMajor -ge 14) -or $IsLinux -or $Instance.HostPlatform -eq 'Linux') {
$Agent = @($Instance.Query("SELECT * FROM sys.dm_server_services") | Where-Object servicename -like '*Agent*').Foreach{
[PSCustomObject]@{
State = $PSItem.status_desc
StartMode = $PSItem.startup_type_desc
}
}
} else { # Windows
$Agent = @(Get-DbaService -ComputerName $Instance.ComputerName -Type Agent)
}
}
'DbaOperator' {

Expand Down Expand Up @@ -97,7 +112,11 @@ function Get-AllAgentInfo {
ComputerName = $Instance.ComputerName
InstanceName = $Instance.DbaInstanceName
Name = $Instance.Name
DatabaseMailEnabled = $Instance.Configuration.DatabaseMailEnabled.RunValue
ConfigValues = @($ConfigValues)
HostPlatform = $Instance.HostPlatform
IsClustered = $Instance.IsClustered
DatabaseMailEnabled = $Instance.Configuration.DatabaseMailEnabled.ConfigValue
Agent = @($Agent)
}
return $testInstanceObject
}

0 comments on commit a45b19b

Please sign in to comment.