During JEA presentation Jeffrey Snover asnwered to one of the questions from audience that there is a way to attach to closing session event somehow during start up profile. He did not go into details but I'm wondering what exactly I need to be researching to find how it's supposed to be done? I beleive this is built in functionality into standard PS endpoint and not something specific what JEA is added.
But this is (nominally) C# programming, because if PowerShell isn't running, PowerShell can't do stuff. This is the thing Snover was talking about, at least (I just pinged him). It's a little out of scope for us to help you with here, but the StackOverflow post might be a starting point if you're up with the C# side of things.
Now, there are probably some hacky workarounds I could think of, but they'd all come with some caveats.