-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Register-EngineEvent Powershell.Exiting does not work. #8000
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This may be related to #6862, perhaps? |
Any news on this? I am still experiencing this issue with the current preview.
|
Oddly enough, the following code works. But only if PS is exited via Register-EngineEvent -SourceIdentifier PowerShell.Exiting -Action {
"PowerShell exited at {0}" -f (Get-Date) |
Out-File -FilePath "$env:TEMP\powershell.log" -Append
} |
There are several factors at play here:
At the very least, the documentation needs to be improved to clarify the constraints: I've asked for that in MicrosoftDocs/PowerShell-Docs#4061. Here's a test command via the CLI that demonstrates that the handler is invoked, but fails when # Call from PowerShell itself or Bash.
pwsh -nop -c '$null = Register-EngineEvent -SourceIdentifier Powershell.Exiting -Action { write-host before; try { Set-Content -ErrorAction stop -Value (Get-Date) -Path ~/t.txt } catch { write-host $_.ToString() }; write-host after }'
# Call from cmd.exe.
pwsh -nop -c "$null = Register-EngineEvent -SourceIdentifier Powershell.Exiting -Action { write-host before; try { Set-Content -ErrorAction stop -Value (Get-Date) -Path ~/t.txt } catch { write-host $_.ToString() }; write-host after }" The above yields:
|
@mklement0 Michael, thanks for addressing this. Your explanations make sense. I'm not sure what to do with this issue, though. On one hand, this is actually by design and I probably should close my ticket. On the other hand, the design should be improved in my opinion. The exiting Register-EngineEvent should be fired before anything is unloaded. The PowerShell would greatly benefit from that. The use-case where I actually found out about this event was to disconnect from all open Office 365 sessions when you forgot to explicitly disconnect before closing the Window. So you don't end up being locked out until the stale sessions expire. Which doesn't work when the modules are already unloaded when the event fires. Another improvement would be to pass the close-event from the window to the Powershell, so it fires when you close by ALT-F4 or pressing the X. Most PowerShell users are using it on Windows and not on Linux, where you would actually have to run the |
All good points, @Borkason. I suggest we get started here by asking for feedback from a subject-matter expert about the original design intent, what could be improved, and what cannot be changed for technical reasons. @SteveL-MSFT, can you help? |
I'm having the same issue, and looking forward to see any progress on enhancement. |
The Win32 Console api su 8000 pports notifying a console application that it is closing and PowerShell is supposed to receive that notifcation here. I know this mechanism works because |
I tested with
|
I tried this (explicit |
That's strange. What code did you use? Edited my previous comment to note that it was only on Windows. |
I used your example above: Register-EngineEvent -SourceIdentifier PowerShell.Exiting -Action {Set-Content $env:TEMP/test.txt "PowerShell died."} I only checked with explicit |
I can confirm that with PowerShell 7-Preview.2 the issue is fixed, when using I even tried a command that takes considerably more time. Register-EngineEvent -SourceIdentifier Powershell.Exiting -Action { Set-Content -Value (get-module -ListAvailable -All) -Path "$env:TEMP\General Kenobi.txt" }
exit |
@SteveL-MSFT any update on this? It would be great if we would see this working when PowerShell 7 is released. This would be a great benefit for automatically gracefully terminating any forgotten open PSSession before the window is closed. Especially helpful when working with Exchange Online or other Office 365 services that block you out if you forgot to close the sessions. Ps, happy belated birthday to this issue! 🎂 🎁 |
I also just noticed that this is labeled as a discussion, but shouldn't it be a bug if the Exiting event does not trigger when closing the PowerShell with the X button? |
Looks like a bug to me; the code that is supposed to handle this scenario is there but it is not working. |
Any updates so far ? |
Just an FYI. Subbing to this thread as well. Found this while currently working on a script with the same issue. Compiled to a .exe it works but PowerShell ISE throws exceptions. The app being designed is without GUI and is designed to work in the background as an Tasktray icon. If you right click the Tasktray the icon scripted and select the manual exit from the generated forms submenu, it works. If you open the "addition info" prompt from the submenu and close with the X instead of a graceful exit it throws exceptions... Would be nice if we could handle the closure event with the X. |
In my scenario, I tried to use |
I am finding a need for this too, just in order to properly trigger clean up functions from the profile. @SteveL-MSFT What are the main blockers for this? |
It looks like the process is killed before the handler has a chance to run. |
It also appears that .NET has added support for POSIX signals (and translating Windows events), so maybe that can be leveraged https://github.com/dotnet/runtime/blob/904ab9a034c0d8f3ef33579db79c87a6fad70acd/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/PosixSignalRegistration.Windows.cs#L87 |
Ah, nice! Is the current, Windows-specific behaviour a bug, or a feature? |
I need this as well for cleanup purposes for my project, so I would love if this could please be considered in a future release. If the goal is full parity with Windows PowerShell, Core should take it if they can, if not, should remove the feature from Core. As it stands people read the documentation and get confused when it doesn't work. |
Agree! I have the same purpose for cleanup, but the documentation is really confusing. 😮💨 |
… when the shell open. When powershell is closed via the x button, this event will not be fired. See PowerShell/PowerShell#8000
… when the shell open. When powershell is closed via the x button, this event will not be fired. See PowerShell/PowerShell#8000
Any updates on getting this pushed to stable? |
@danmacode this appears to work now (Powershell 7.4.6): # Helper to register disconnecting exchange when closing
function Register-DisconnectExchange {
$null = Register-EngineEvent PowerShell.Exiting –Action {
Write-ColoredMessage "Disconnecting from Exchange Online..." "Yellow"
try {
Disconnect-ExchangeOnline -Confirm:$false
}
catch {
Write-ColoredMessage "Failed to disconnect from Exchange Online." "Red"
}
}
} |
@5HT2 sadly, it does not work for me (PowerShell
Will try with EDIT: I tried in WSL with |
Uh oh!
There was an error while loading. Please reload this page.
Steps to reproduce
Instead of exit, the user may also close the Powershell with the x-Button.
Expected behavior
I expect that Powershell executes every command in the
-Action
parameter before it exits. In this case, I expected that a fileGeneral Kenobi.txt
would have been written to$env:TEMP
.Actual behavior
Powershell does not execute the action in any case. Not when exiting via
exit
command and not when exiting by pressing the x-button of the powershell console window.This not only happens in PS6 but also in PS5.
Environment data
The text was updated successfully, but these errors were encountered: