In previous posts I have shown how to use Test-DbaLastBackup from dbatools and how you can make use of the results. Today we will look at using Pester with the results
Pester provides a framework for running unit tests to execute and validate PowerShell commands from within PowerShell. Pester consists of a simple set of functions that expose a testing domain-specific language (DSL) for isolating, running, evaluating and reporting the results of PowerShell commands.
we shall use it to validate our results. First we need to gather our results as we have seen before, In this example I have set the MaxMb to 5 so change that if you are playing along
Import-Module dbatools $TestServer = 'SQL2016N1' $Server = 'SQL2016N2' $servers = 'SQL2016N1','SQL2016N2' $Results = $servers.ForEach{Test-DbaLastBackup -SqlServer $_ -Destination $TestServer -MaxMB 5}
Describe "Last Backup Test results - NOTE THIS IGNORES Skipped restores,DBCC and BackupFiles" { foreach($result in $results) { It "$($Result.Database) on $($Result.SourceServer) File Should Exist" { $Result.FileExists| Should Not Be 'False' } It "$($Result.Database) on $($Result.SourceServer) Restore should be Success" { $Result.RestoreResult| Should Not Be 'False' } It "$($Result.Database) on $($Result.SourceServer) DBCC should be Success" { $Result.DBCCResult| Should Not Be 'False' } It "$($Result.Database) on $($Result.SourceServer) Backup Should be less than a week old" { $Result.BackupTaken| Should BeGreaterThan (Get-Date).AddDays(-7) } }
Invoke-Pester C:\temp\BackupPester.ps1
$Date = Get-Date -Format ddMMyyyHHmmss $tempFolder = 'c:\temp\BackupTests\' Push-Location $tempFolder $XML = $tempFolder + "BackupTestResults_$Date.xml" $script = 'C:\temp\BackupPester.ps1' Invoke-Pester -Script $Script -OutputFile $xml -OutputFormat NUnitXml
#download and extract ReportUnit.exe $url = 'http://relevantcodes.com/Tools/ReportUnit/reportunit-1.2.zip' $fullPath = Join-Path $tempFolder $url.Split("/")[-1] $reportunit = $tempFolder + '\reportunit.exe' if((Test-Path $reportunit) -eq $false) { (New-Object Net.WebClient).DownloadFile($url,$fullPath) Expand-Archive -Path $fullPath -DestinationPath $tempFolder }
##run reportunit against report.xml and display result in browser $HTML = $tempFolder + 'index.html' & .\reportunit.exe $tempFolder Invoke-Item $HTML
NOTE – The major 1.0 release of dbatools due in the summer 2017 may have breaking changes which will stop the above code from working. There are also new commands coming which may replace this command. This blog post was written using dbatools version 0.8.942 You can check your version using
Get-Module dbatools
and update it using an Administrator PowerShell session with
Update-Module dbatools
You may find that you get no output from Update-Module as you have the latest version. If you have not installed the module from the PowerShell Gallery using
Install-Module dbatools
Then you can use
Update-dbatools