Created
December 18, 2018 12:53
-
-
Save nohwnd/fb9afb40d565a2226ac331d515de7f08 to your computer and use it in GitHub Desktop.
Apply a remedy after a Pester test fails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Apply a remedy after a Pester test fails to fix the problem that caused | |
# the test to fails (eg. all inactive AD users should be disabled, | |
# so a remedy is to disable all inactive users) | |
function RetryAndFix ([scriptBlock]$Test, [scriptBlock]$Fix, $Wait) { | |
try { | |
& $Test | |
} | |
catch { | |
"Failed with $_, fixing." | |
$failed = $true | |
try { | |
&$Fix | |
} | |
catch { | |
throw "Fix failed" | |
} | |
Write-Host "Waiting for $Wait seconds..." | |
Start-Sleep -Seconds $Wait | |
try { | |
&$Test | |
$fixed = $true | |
} | |
catch { | |
} | |
finally { | |
if ($failed -and $fixed) { | |
Write-Host "Remedy applied" | |
} | |
} | |
} | |
} | |
$script:someSetting = $false | |
RetryAndFix -Test { $script:someSetting | Should -BeTrue } -Fix { $script:someSetting=$true } -Wait 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment