Skip to content

Instantly share code, notes, and snippets.

@nohwnd
Created December 18, 2018 12:53
Show Gist options
  • Save nohwnd/fb9afb40d565a2226ac331d515de7f08 to your computer and use it in GitHub Desktop.
Save nohwnd/fb9afb40d565a2226ac331d515de7f08 to your computer and use it in GitHub Desktop.
Apply a remedy after a Pester test fails
# 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