Skip to content

Instantly share code, notes, and snippets.

@alx9r
Last active December 19, 2024 00:35
Show Gist options
  • Save alx9r/f68f06828f33b6e611840fc5ce180ae7 to your computer and use it in GitHub Desktop.
Save alx9r/f68f06828f33b6e611840fc5ce180ae7 to your computer and use it in GitHub Desktop.
Proof-of-concept of workaround for PowerShell/PowerShell#24679
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