Skip to content

Instantly share code, notes, and snippets.

@niaher
Created April 4, 2014 11:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niaher/9972477 to your computer and use it in GitHub Desktop.
Save niaher/9972477 to your computer and use it in GitHub Desktop.
function Invoke-Using {
param (
[Parameter(Mandatory=$true)]
[System.IDisposable]$Disposable,
[Parameter(Mandatory=$true)]
[ScriptBlock] $Script
)
Try {
&$Script
} Finally {
if ($Disposable -ne $null) {
if ($Disposable.psbase -eq $null) {
$Disposable.Dispose()
} else {
$Disposable.psbase.Dispose()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment