Skip to content

Instantly share code, notes, and snippets.

@codingawayy
Created April 4, 2014 11:14
Show Gist options
  • Save codingawayy/9972477 to your computer and use it in GitHub Desktop.
Save codingawayy/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