3

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.

  • A script can return at various points though (eg, no records to process), so the current situation is problematic.
  • 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.

2
  • 2
    Does "try-finally" block set suit you? Commented Aug 20, 2015 at 9:41
  • @Vesper if you write it as an answer I'll remove mine. Commented Aug 20, 2015 at 10:43

1 Answer 1

5

@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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.