I have some lengthy scripts which involve dynamic processes that I'd like to guarantee close, e.g.,
- start-transcript --> stop-transcript
- [io.StreamReader]$file --> $file.close(); $file.dispose()
- setting an environment variable for the script duration.
I think the environment variable can be set to process scope to enforce it to be temporary and annulled at the culmination of the script; however, for the other two, it would be very convenient to have something like a finally block that would correctly end these processes even in the event of an error or manual cancellation.
The finally block is perfect for smaller scripts, but in an entire module or longer script, it looks inelegant to set the entire module/script in a try block just to squeeze in a few lines of a finally block at the end.
Is there a way to achieve the guaranteed execution of a finally block without needing to use a finally block and by association a try block?
PS(5.1.19041)