From a45b19bb59a6d99e07b13bbb34448677645634f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A1udio=20Silva?= Date: Fri, 29 Apr 2022 18:03:19 +0100 Subject: [PATCH] add SQL Agent Account (still working on #887) --- checks/Agentv5.Tests.ps1 | 36 ++++++++++++++++++++++--- internal/functions/Get-AllAgentInfo.ps1 | 23 ++++++++++++++-- 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/checks/Agentv5.Tests.ps1 b/checks/Agentv5.Tests.ps1 index 7b7043b3..05059213 100644 --- a/checks/Agentv5.Tests.ps1 +++ b/checks/Agentv5.Tests.ps1 @@ -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" { @@ -466,4 +491,7 @@ Describe "Database Mail XPs" -Tag DatabaseMailEnabled, CIS, security -ForEach $I # } # } # } -# } \ No newline at end of file +# } + + + diff --git a/internal/functions/Get-AllAgentInfo.ps1 b/internal/functions/Get-AllAgentInfo.ps1 index abefc1cc..2ce477ee 100644 --- a/internal/functions/Get-AllAgentInfo.ps1 +++ b/internal/functions/Get-AllAgentInfo.ps1 @@ -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' { @@ -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 } \ No newline at end of file