Reputation: 1720
Instead of calling a function at the end of all scripts to perform cleanup tasks, I'm looking to register for an 'on return' event for when the script (not the PowerShell session) is finished.
Register-EngineEvent
applies to the PowerShell session, and operators run scripts manually, thus it's problematic.I can't find a list of built-in powershell events or an alternative solution.
Upvotes: 3
Views: 2676
Reputation: 47792
@Vesper wrote it as a comment, but a try
/finally
block is definitely what I would suggest for this:
try {
# some code
} finally {
# this gets executed even if the code in the try block throws an exception
}
Upvotes: 5