Sometimes you want to check something in your SQL environment that’s not yet included in the dbachecks module. Luckily, it is quite easy to extend it’s functionality.
Let’s start by creating a file that will contain your own checks:
1 |
New-Item -Path c:\MyChecks\ContosoChecks.Tests.ps1 -ItemType File -Force |
Now add the following to the newly created file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
$filename = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "") @(Get-Instance).ForEach{ Describe "Contoso DBA Database Configuration" -Tags Contoso, $filename { Context -Name "DBA database on $psitem" { It "DBA database should be present" { (Get-DbaDatabase -SqlInstance $psitem -Database DBA | Measure-Object).Count | Should -Be 1 -Because "It should be there" } It "DBA should contain sp_WhoIsActive" { ((Get-DbaDatabase -SqlInstance $psitem -Database DBA).StoredProcedures | Where-Object -Property Name -eq 'sp_WhoIsActive' | Measure-Object).Count | Should -Be 1 -Because "It should be there" } } } } |
In the Describe block I added ‘Contoso’ to the Tags parameter. You can use this tag to include (or exclude) specific tests.
All that’s left to do is to register your custom repository:
1 |
Set-DbcConfig -Name app.checkrepos -Value 'c:\MyChecks\' -Append |
Now you can invoke the checks by running:
1 |
Invoke-DbcCheck -SqlInstance localhost\SQL2017 -ComputerName localhost -Tags Contoso |