在 Powershell 中优雅地停止 [英] Gracefully stopping in Powershell
本文介绍了在 Powershell 中优雅地停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 PowerShell 脚本中捕获和处理 Ctrl+C?我知道我可以通过包含对 Powershell.Stop()
方法的覆盖来从 v2 中的 cmdlet 执行此操作,但我找不到可在脚本中使用的模拟.
How do I catch and handle Ctrl+C in a PowerShell script? I understand that I can do this from a cmdlet in v2 by including an override for the Powershell.Stop()
method, but I can't find an analog for use in scripts.
我目前正在通过 end
执行清理块,但是当脚本被取消时我需要执行额外的工作(而不是运行到完成).
I'm currently performing cleanup via an end
block, but I need to perform additional work when the script is canceled (as opposed to run to completion).
推荐答案
您可以使用 在 PoshCode 上
总结:
设置
[console]::TreatControlCAsInput = $true
然后使用
if($Host.UI.RawUI.KeyAvailable -and (3 -eq
[int]$Host.UI.RawUI.ReadKey("AllowCtrlC,IncludeKeyUp,NoEcho").Character))
这篇关于在 Powershell 中优雅地停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文