Skip to content

Instantly share code, notes, and snippets.

@mjvh80
Created August 14, 2014 11:36
Show Gist options
  • Save mjvh80/9c826b3e0de4e32ce208 to your computer and use it in GitHub Desktop.
Save mjvh80/9c826b3e0de4e32ce208 to your computer and use it in GitHub Desktop.
PowerShell cleanup
$cleanup = {}
trap { &$cleanup; }
# do stuff
$cleanup = add-cleanop { <# delete stuff #> } $cleanup;
function add-cleanop($cleanop, $existing = {}) {
{
try {
&$cleanop;
} catch {
Write-Warning "Cleanup failed: $_";
}
&$existing;
}.GetNewClosure();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment