From 3ea5f4b8c4d9da3e8cb83184a444fb1ca11ab0e2 Mon Sep 17 00:00:00 2001 From: jpomfret Date: Sun, 5 Jun 2022 16:34:05 +0100 Subject: [PATCH] #884 database status --- checks/Databasev5.Tests.ps1 | 19 ++++++++++++++++++- internal/configurations/configuration.ps1 | 2 ++ internal/functions/Get-AllDatabaseInfo.ps1 | 14 +++++++++++++- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/checks/Databasev5.Tests.ps1 b/checks/Databasev5.Tests.ps1 index 5b959173..fa76a873 100644 --- a/checks/Databasev5.Tests.ps1 +++ b/checks/Databasev5.Tests.ps1 @@ -173,10 +173,27 @@ Describe "Auto Update Statistics Asynchronously" -Tag AutoUpdateStatisticsAsynch Describe "Trustworthy Option" -Tag Trustworthy, DISA, Varied, CIS, Database -ForEach $InstancesToTest { $skip = Get-DbcConfigValue skip.database.trustworthy - + Context "Testing database trustworthy option on <_.Name>" { It "Database <_.Name> should have Trustworthy set to false on <_.SqlInstance>" -Skip:$skip -ForEach $psitem.Databases.Where{ if ($Database) { $_.Name -in $Database } else { $psitem.ConfigValues.trustworthyexclude -notcontains $PsItem.Name } } { $psitem.Trustworthy | Should -BeFalse -Because "Trustworthy has security implications and may expose your SQL Server to additional risk" } } +} + +Describe "Database Status" -Tag DatabaseStatus, High, Database -ForEach $InstancesToTest { + $skip = Get-DbcConfigValue skip.database.status + + Context "Database status is correct on <_.Name>" { + It "Database <_.Name> has the expected status on <_.SqlInstance>" -Skip:$skip -ForEach $psitem.Databases.Where{ if ($Database) { $_.Name -in $Database } else { $psitem.ConfigValues.statusexclude -notcontains $PsItem.Name } } { + $psitem.Where{$_.Name -notin $psitem.ConfigValues.excludereadonly -and $psitem.IsDatabaseSnapshot -eq $false}.Readonly | Should -Not -Contain True -Because "We expect that there will be no Read-Only databases except for those specified" + $psitem.Where{$_.Name -notin $psitem.ConfigValues.excludeoffline}.Status | Should -Not -Match 'Offline' -Because "We expect that there will be no offline databases except for those specified" + $psitem.Where{$_.Name -notin $psitem.ConfigValues.excluderestoring}.Status | Should -Not -Match 'Restoring' -Because "We expect that there will be no databases in a restoring state except for those specified" + $psitem.Where{$_.Name -notin $psitem.ConfigValues.excludeoffline}.Status | Should -Not -Match 'AutoClosed' -Because "We expect that there will be no databases that have been auto closed" + $psitem.Status | Should -Not -Match 'Recover' -Because "We expect that there will be no databases going through the recovery process or in a recovery pending state" + $psitem.Status | Should -Not -Match 'Emergency' -Because "We expect that there will be no databases in EmergencyMode" + $psitem.Status | Should -Not -Match 'Standby' -Because "We expect that there will be no databases in Standby" + $psitem.Status | Should -Not -Match 'Suspect' -Because "We expect that there will be no databases in a Suspect state" + } + } } \ No newline at end of file diff --git a/internal/configurations/configuration.ps1 b/internal/configurations/configuration.ps1 index 5a89cee2..4d79521f 100644 --- a/internal/configurations/configuration.ps1 +++ b/internal/configurations/configuration.ps1 @@ -237,6 +237,7 @@ Set-PSFConfig -Module dbachecks -Name policy.logfilecount.excludedb -Value @() Set-PSFConfig -Module dbachecks -Name policy.autocreatestats.excludedb -Value @() -Initialize -Description "Databases to exclude from the auto create stats checks" Set-PSFConfig -Module dbachecks -Name policy.autoupdatestats.excludedb -Value @() -Initialize -Description "Databases to exclude from the auto update stats checks" Set-PSFConfig -Module dbachecks -Name policy.autoupdatestatisticsasynchronously.excludedb -Value @() -Initialize -Description "Databases to exclude from the auto update stats asynchronously checks" +Set-PSFConfig -Module dbachecks -Name policy.database.statusexcludedb -Value @() -Initialize -Description "Databases to exclude from the database status checks" @@ -268,6 +269,7 @@ Set-PSFConfig -Module dbachecks -Name skip.database.autocreatestatistics -Valida Set-PSFConfig -Module dbachecks -Name skip.database.autoupdatestatistics -Validation bool -Value $false -Initialize -Description "Skip the auto update statistics test" Set-PSFConfig -Module dbachecks -Name skip.database.autoupdatestatisticsasynchronously -Validation bool -Value $false -Initialize -Description "Skip the auto update statistics asynchronously test" Set-PSFConfig -Module dbachecks -Name skip.database.trustworthy -Validation bool -Value $false -Initialize -Description "Skip the trustworthy database test" +Set-PSFConfig -Module dbachecks -Name skip.database.status -Validation bool -Value $false -Initialize -Description "Skip the database status test" Set-PSFConfig -Module dbachecks -Name skip.logshiptesting -Validation bool -Value $false -Initialize -Description "Skip the logshipping test" diff --git a/internal/functions/Get-AllDatabaseInfo.ps1 b/internal/functions/Get-AllDatabaseInfo.ps1 index 17c6c1ae..77eed249 100644 --- a/internal/functions/Get-AllDatabaseInfo.ps1 +++ b/internal/functions/Get-AllDatabaseInfo.ps1 @@ -114,7 +114,15 @@ function Get-AllDatabaseInfo { } 'Trustworthy' { $trustworthy = $true - $ConfigValues | Add-Member -MemberType NoteProperty -Name 'trustworthyexclude' -Value (Get-DbcConfigValue policy.database.trustworthyexcludedb) + $ConfigValues | Add-Member -MemberType NoteProperty -Name 'trustworthyexclude' -Value (Get-DbcConfigValue policy.database.trustworthyexcludedb) + } + 'DatabaseStatus' { + $status = $true + $ConfigValues | Add-Member -MemberType NoteProperty -Name 'excludereadonly' -Value (Get-DbcConfigValue policy.database.status.excludereadonly) + $ConfigValues | Add-Member -MemberType NoteProperty -Name 'excludeoffline' -Value (Get-DbcConfigValue policy.database.status.excludeoffline) + $ConfigValues | Add-Member -MemberType NoteProperty -Name 'excluderestoring' -Value (Get-DbcConfigValue policy.database.status.excluderestoring) + $ConfigValues | Add-Member -MemberType NoteProperty -Name 'statusexclude' -Value (Get-DbcConfigValue policy.database.statusexcludedb) + } Default { } } @@ -144,6 +152,10 @@ function Get-AllDatabaseInfo { VLF = if ($vlf) { ($psitem.Query("DBCC LOGINFO") | Measure-Object).Count } LogFileCount = if ($logfilecount) { ($psitem.LogFiles | Measure-Object).Count } Trustworthy = if ($trustworthy) { $psitem.Trustworthy } + Status = if ($status) { $psitem.Status } + IsDatabaseSnapshot = if ($status) { $psitem.IsDatabaseSnapshot } # needed for status test + Readonly = if ($status) { $psitem.Readonly } # needed for status test + } } }