简体   繁体   English

PowerShell ISE和PowerShell.Exiting事件

[英]PowerShell ISE and PowerShell.Exiting event

I'm using the PowerShell.Exiting engine event to save my command history and do some other tidying up when I close a PowerShell session, registering it in my profile thus: 我正在使用PowerShell.Exiting引擎事件保存我的命令历史记录,并在关闭PowerShell会话时进行其他一些整理,然后在我的配置文件中注册它:

# Set up automatic functionality on engine exit.
Register-EngineEvent PowerShell.Exiting -SupportEvent -Action `
{
    #stuff
    ...
}

This works perfectly when I use PowerShell in a console window, but when I'm running PowerShell in ISE, it appears that the PowerShell.Exiting event somehow never fires, since nothing I put in there, be it the usual stuff or test code, ever runs. 当我在控制台窗口中使用PowerShell时,这非常有效,但是当我在ISE中运行PowerShell时,看起来PowerShell.Exiting事件似乎永远不会触发,因为我没有放入任何内容,无论是通常的东西还是测试代码,曾经跑过。

Is this a known problem, and if so, is there a known workaround or alternative? 这是一个已知问题,如果是,是否有已知的解决方法或替代方案?

Well, this is weird as hell. 嗯,这很奇怪。

After cutting down the profile completely outside the Register-EngineEvent call only to find that it still didn't work, I started to cut down the contents of that, too, and restored the rest of the profile to its original state. 在完成在Register-EngineEvent调用之外完全删除配置文件后发现它仍然无法工作时,我也开始减少其内容,并将配置文件的其余部分恢复到其原始状态。 Here are my findings: 以下是我的发现:

If you have a write-host , or other output to the PowerShell host, in the scriptblock for Register-EngineEvent PowerShell.Exited , it doesn't run when you exit ISE (even though it works fine when you exit the console). 如果您有PowerShell主机的write-host或其他输出,则在Register-EngineEvent PowerShell.Exited ,当您退出ISE时它不会运行(即使它在退出控制台时工作正常)。

In fact, none of the scriptblocks you have registered against the PowerShell.Exited event appear to run, even ones that don't contain any statements outputting to the host. 事实上, 没有任何您已注册对PowerShell.Exited事件scriptblocks出现运行,甚至不包含输出到主机中的任何陈述的。 (Which is why when I tested other people's working examples up above, they didn't work for me unless I started PowerShell without running the profile that added the existing event handler.) (这就是为什么当我在上面测试其他人的工作示例时,除非我在没有运行添加现有事件处理程序的配置文件的情况下启动PowerShell,否则它们不适合我。)

Expunge all statements that cause host output from the scriptblocks you use with Register-EngineEvent PowerShell.Exited , or redirect the output somewhere else, and they all start working. 从使用Register-EngineEvent PowerShell.Exited导致主机输出的所有语句,或者将输出重定向到其他位置,它们都开始工作。

(Take this with appropriate quantities of salt, since I have not yet had time to go chasing it with a debugger, but I have a sneaking suspicion ISE is closing down its tab before the engine is done with it...) (带上适量的盐,因为我还没来得及用调试器来追逐它,但我有一个潜在的怀疑,ISE正在关闭它的标签,然后发动机完成它...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM