Last active
December 19, 2024 00:35
-
-
Save alx9r/f68f06828f33b6e611840fc5ce180ae7 to your computer and use it in GitHub Desktop.
Proof-of-concept of workaround for PowerShell/PowerShell#24679
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
using System.Management.Automation; | |
using System.Collections.Generic; | |
public static class workaround { | |
public static void Clean (ScriptBlock clean) { | |
var errorActionPreference = new PSVariable("ErrorActionPreference","SilentlyContinue"); | |
ScriptBlock.Create(@" | |
param($clean) | |
try { | |
. $clean 1 | |
$__cleanupCompleted = $true | |
} | |
finally { | |
if (-not $__cleanupCompleted) { | |
. $clean 2 | |
} | |
} | |
").InvokeWithContext( | |
null, | |
new List<PSVariable>{errorActionPreference}, | |
clean); | |
} | |
public static void Finally(ScriptBlock _finally) { | |
Clean(_finally); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment