Skip to content

Instantly share code, notes, and snippets.

@koohq
Created November 3, 2017 18:46
Show Gist options
  • Save koohq/8a8763b14eb17f12643f832222f19781 to your computer and use it in GitHub Desktop.
Save koohq/8a8763b14eb17f12643f832222f19781 to your computer and use it in GitHub Desktop.
A helper script to provide the method like "using" statement
<##
# Use.ps1
#
# (c) 2016 koohq. Licensed under CC0.
# https://creativecommons.org/publicdomain/zero/1.0/legalcode
#>
function Use
{
[CmdletBinding()]
[OutputType()]
param(
[Parameter(mandatory)]
[IDisposable]$obj,
[Parameter(mandatory)]
[scriptblock]$sb
)
try
{
& $sb
}
finally
{
if ($null -ne $obj)
{
$obj.Dispose()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment