Skip to content

Commit

Permalink
#884 database status
Browse files Browse the repository at this point in the history
  • Loading branch information
jpomfret committed Jun 5, 2022
1 parent 709c61f commit 3ea5f4b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
19 changes: 18 additions & 1 deletion checks/Databasev5.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
2 changes: 2 additions & 0 deletions internal/configurations/configuration.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"



Expand Down Expand Up @@ -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"
Expand Down
14 changes: 13 additions & 1 deletion internal/functions/Get-AllDatabaseInfo.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 { }
}
Expand Down Expand Up @@ -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

}
}
}
Expand Down

0 comments on commit 3ea5f4b

Please sign in to comment.