Pester – Operational validation

I’ve started using Pester for practical validation scenarios in our company. I recently discovered that someone from our SQL consulting company has disabled the firewall on all of our Windows Server 2012 R2 machines that run SQL Server 2014 – a thing that I’m not going to tolerate.

Here’s the firewall test code. You’ll see that I’m using a Pester feature called Testcases in order to minimize redundant code. Unfortunately, test cases are not described in the Pester wiki on github – I believe they’d be more widely used if that were the case.


param($ComputerName = $env:computername)
Describe 'Firewall is enabled' {
BeforeAll {
$session = New-PSSession -ComputerName $ComputerName
$Param = @{Session=$session}
}
$testCases = @(
@{
FWProfile = 'DomainProfile'
Description = 'Domain Profile'
}
@{
FWProfile = 'PublicProfile'
Description = 'Public Profile'
}
@{
FWProfile = 'StandardProfile'
Description = 'Private Profile'
}
)
It 'tests if the <Description> is enabled' -TestCases $testCases {
param ($FWProfile)
$Expected = 1
$Actual = Invoke-Command @Param -ScriptBlock { (Get-ItemProperty "HKLM:\System\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\${using:FWProfile}" -Name EnableFirewall).EnableFirewall }
$actual | Should Be $Expected
}
AfterAll {
Remove-PSSession -Session $session
}
}

PS ov:\> ls


    Directory: D:\Pester
          
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       14.02.2016     13:30           2000 FirewallStatus.Tests.ps1


PS ov:\> Invoke-Pester


Describing Firewall is enabled
        
 [+] tests if the Domain Profile is enabled 3.02s
 [+] tests if the Public Profile is enabled 199ms
 [+] tests if the Private Profile is enabled 211ms

Tests completed in 3.43s
Passed: 3 Failed: 0 Skipped: 0 Pending: 0 Inconclusive: 0
PS ov:\> Invoke-Pester -Script @{ Path = '.'; Parameters = @{ ComputerName = 'localhost' } }


Describing Firewall is enabled 

 [+] tests if the Domain Profile is enabled 2.95s
 [+] tests if the Public Profile is enabled 168ms
 [+] tests if the Private Profile is enabled 218ms

Tests completed in 3.34s
Passed: 3 Failed: 0 Skipped: 0 Pending: 0 Inconclusive: 0

Examples

Stay tuned for my post-deployment validation tests.