Created
November 3, 2017 18:46
-
-
Save koohq/8a8763b14eb17f12643f832222f19781 to your computer and use it in GitHub Desktop.
A helper script to provide the method like "using" statement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<## | |
# 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