From 040f35f386bcf964a2a8060ccb74c2f3f7ab6e2e Mon Sep 17 00:00:00 2001 From: SQLDbaWithABeard Date: Mon, 16 May 2022 15:39:25 +0100 Subject: [PATCH] whoisactive check added and tested #882 --- Perf Testing pesterv5.ps1 | 8 ++++---- Validate v4 adn v5.ps1 | 4 ++-- checks/Instancev5.Tests.ps1 | 9 +++++++++ internal/functions/NewGet-AllInstanceInfo.ps1 | 16 ++++++++++++++-- 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/Perf Testing pesterv5.ps1 b/Perf Testing pesterv5.ps1 index d7cc1135..57872f5a 100644 --- a/Perf Testing pesterv5.ps1 +++ b/Perf Testing pesterv5.ps1 @@ -14,8 +14,8 @@ ipmo ./dbachecks.psd1 # -$Checks = 'TraceFlagsExpected','TwoDigitYearCutoff','MaxDopInstance','ErrorLogCount','ModelDbGrowth','DefaultBackupCompression','SaExist','SaDisabled','SaRenamed','DefaultFilePath','AdHocDistributedQueriesEnabled','AdHocWorkload', 'DefaultTrace', 'OleAutomationProceduresDisabled', 'CrossDBOwnershipChaining', 'ScanForStartupProceduresDisabled', 'RemoteAccessDisabled', 'SQLMailXPsDisabled', 'DAC', 'OLEAutomation' - +$Checks = 'WhoIsActiveInstalled','CLREnabled','TraceFlagsNotExpected','TraceFlagsExpected','TwoDigitYearCutoff','MaxDopInstance','ErrorLogCount','ModelDbGrowth','DefaultBackupCompression','SaExist','SaDisabled','SaRenamed','DefaultFilePath','AdHocDistributedQueriesEnabled','AdHocWorkload', 'DefaultTrace', 'OleAutomationProceduresDisabled', 'CrossDBOwnershipChaining', 'ScanForStartupProceduresDisabled', 'RemoteAccessDisabled', 'SQLMailXPsDisabled', 'DAC', 'OLEAutomation' +$Checks = 'WhoIsActiveInstalled' <# When there are default skips (some of the CIS checks) we need to set the configs and check @@ -64,7 +64,7 @@ function Compare-CheckRuns { param($Checks) $password = ConvertTo-SecureString "dbatools.IO" -AsPlainText -Force $cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "sqladmin", $password -$Sqlinstances = 'localhost,14333', 'localhost,14334' #'localhost,7401', 'localhost,7402', 'localhost,7403' +$Sqlinstances = 'localhost,7401', 'localhost,7402', 'localhost,7403' $originalCode = { Invoke-DbcCheck -SqlInstance $Sqlinstances -Check $Checks -SqlCredential $cred -legacy $true -Show None @@ -101,5 +101,5 @@ Write-PSFMessage -Message $savingMessage -Level Output } -$Checks = 'DbaOperator' +# $Checks = 'DbaOperator' Compare-CheckRuns -Checks $checks \ No newline at end of file diff --git a/Validate v4 adn v5.ps1 b/Validate v4 adn v5.ps1 index a15fc73c..594bbbb4 100644 --- a/Validate v4 adn v5.ps1 +++ b/Validate v4 adn v5.ps1 @@ -15,8 +15,8 @@ RUN THIS SECTION MANUALLY IF YOU JUST IMPORT THE FUNCTION BELOW! ipmo ./dbachecks.psd1 -$Checks = 'CLREnabled','TraceFlagsNotExpected','TraceFlagsExpected','TwoDigitYearCutoff','MaxDopInstance','ErrorLogCount','ModelDbGrowth','DefaultBackupCompression','SaExist','SaDisabled','SaRenamed','DefaultFilePath','AdHocDistributedQueriesEnabled','AdHocWorkload', 'DefaultTrace', 'OleAutomationProceduresDisabled', 'CrossDBOwnershipChaining', 'ScanForStartupProceduresDisabled', 'RemoteAccessDisabled', 'SQLMailXPsDisabled', 'DAC', 'OLEAutomation' -$Checks = 'CLREnabled' +$Checks = 'WhoIsActiveInstalled','CLREnabled','TraceFlagsNotExpected','TraceFlagsExpected','TwoDigitYearCutoff','MaxDopInstance','ErrorLogCount','ModelDbGrowth','DefaultBackupCompression','SaExist','SaDisabled','SaRenamed','DefaultFilePath','AdHocDistributedQueriesEnabled','AdHocWorkload', 'DefaultTrace', 'OleAutomationProceduresDisabled', 'CrossDBOwnershipChaining', 'ScanForStartupProceduresDisabled', 'RemoteAccessDisabled', 'SQLMailXPsDisabled', 'DAC', 'OLEAutomation' +$Checks = 'WhoIsActiveInstalled' Compare-v4andv5Results -Checks $Checks # if you need to see the details to see why the results are different diff --git a/checks/Instancev5.Tests.ps1 b/checks/Instancev5.Tests.ps1 index 2facf93f..faa7b558 100644 --- a/checks/Instancev5.Tests.ps1 +++ b/checks/Instancev5.Tests.ps1 @@ -250,3 +250,12 @@ Describe "CLR Enabled" -Tag CLREnabled, security, CIS, High, Instance -ForEach $ } } } + +Describe "sp_whoisactive is Installed" -Tag WhoIsActiveInstalled, Low, Instance -ForEach $InstancesToTest { + $skip = Get-DbcConfigValue skip.instance.WhoIsActiveInstalled + Context "Testing WhoIsActive exists on <_.Name>" { + It "WhoIsActive should exist on <_.ConfigValues.whoisactivedatabase> on <_.Name>" -Skip:$skip { + $Psitem.ConfigValues.WhoIsActiveInstalled | Should -Be 1 -Because "The sp_WhoIsActive stored procedure should be installed in $($psitem.ConfigValues.whoisactivedatabase)" + } + } +} \ No newline at end of file diff --git a/internal/functions/NewGet-AllInstanceInfo.ps1 b/internal/functions/NewGet-AllInstanceInfo.ps1 index e1a73f1c..17e43394 100644 --- a/internal/functions/NewGet-AllInstanceInfo.ps1 +++ b/internal/functions/NewGet-AllInstanceInfo.ps1 @@ -150,7 +150,9 @@ function NewGet-AllInstanceInfo { 'TraceFlagsExpected' { $TraceFlagsExpected = Get-DbcConfigValue policy.traceflags.expected $TraceFlagsActual = $Instance.EnumActiveGlobalTraceFlags() - $ConfigValues | Add-Member -MemberType NoteProperty -Name 'TraceFlagsExpected' -Value $TraceFlagsExpected + if (-not $ConfigValues.TraceFlagsExpected) { + $ConfigValues | Add-Member -MemberType NoteProperty -Name 'TraceFlagsExpected' -Value $TraceFlagsExpected + } $ExpectedTraceFlags = $TraceFlagsExpected.Foreach{ [PSCustomObject]@{ InstanceName = $Instance.Name @@ -170,7 +172,7 @@ function NewGet-AllInstanceInfo { if ($null -eq $TraceFlagsExpected) { $TraceFlagsExpected = 'none expected' } $TraceFlagsActual = $Instance.EnumActiveGlobalTraceFlags() $ConfigValues | Add-Member -MemberType NoteProperty -Name 'TraceFlagsNotExpected' -Value $TraceFlagsNotExpected - if (-not $ConfigValues.ExpectedTraceFlag) { + if (-not $ConfigValues.TraceFlagsExpected) { $ConfigValues | Add-Member -MemberType NoteProperty -Name 'TraceFlagsExpected' -Value $TraceFlagsExpected } $NotExpectedTraceFlags = $TraceFlagsNotExpected.Where{ $_ -notin $TraceFlagsExpected }.Foreach{ @@ -192,6 +194,11 @@ function NewGet-AllInstanceInfo { $configurations = $true $ConfigValues | Add-Member -MemberType NoteProperty -Name 'CLREnabled' -Value (Get-DbcConfigValue policy.security.clrenabled) } + 'WhoIsActiveInstalled' { + $configurations = $true + $WhoIsActiveInstalled = $true + $ConfigValues | Add-Member -MemberType NoteProperty -Name 'whoisactivedatabase' -Value (Get-DbcConfigValue policy.whoisactive.database) + } Default { } } @@ -218,5 +225,10 @@ function NewGet-AllInstanceInfo { $testInstanceObject.Configuration.ScanForStartupProcedures.ConfigValue = 0 } } + if ($WhoIsActiveInstalled) { + $whoisdatabase = Get-DbcConfigValue policy.whoisactive.database + $WhoIsActiveInstalled = $Instance.Databases[$whoisdatabase].StoredProcedures.Where{ $_.Name -eq 'sp_WhoIsActive' }.count + $testInstanceObject.ConfigValues | Add-Member -MemberType NoteProperty -Name 'WhoIsActiveInstalled' -Value $whoIsActiveInstalled + } return $testInstanceObject } \ No newline at end of file