Skip to content

Instantly share code, notes, and snippets.

@jeevan-vj
Created August 8, 2019 02:08
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 jeevan-vj/02c61775391b5f33f5b04c80f9408dc0 to your computer and use it in GitHub Desktop.
Save jeevan-vj/02c61775391b5f33f5b04c80f9408dc0 to your computer and use it in GitHub Desktop.
Function Using-Object(
[System.IDisposable]
$InputObject,
[scriptblock]
$ScriptBlock = {throw "ScriptBlock is mandatory, please provide a value."})
{
try
{
. $ScriptBlock
}
finally
{
if ($null -ne $InputObject -and $InputObject -is [System.IDisposable])
{
$InputObject.Dispose()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment