Skip to content

Instantly share code, notes, and snippets.

@ritalin
Created January 22, 2014 05:42
Show Gist options
  • Save ritalin/8553968 to your computer and use it in GitHub Desktop.
Save ritalin/8553968 to your computer and use it in GitHub Desktop.
function Enter-Using-Undisposable($receiver, [scriptblock]$body, [scriptblock]$finally) {
try {
return $receiver | % { & $body }
}
finally {
& $finally
}
}
function Enter-Using-Excel([scriptblock]$block) {
$excel = New-Object -ComObject Excel.Application
Enter-Using-Undisposable $excel $block {
[Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | out-null
}
}
Enter-Using-Excel {
$_.LargeOperationCellThousandCount
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment