阻止 Powershell 退出
我知道 PowerShell 有一个 -noexit
开关。
有没有办法在不使用该开关的情况下留在 shell 中?
换句话说,我想要一个执行然后让 shell 打开的脚本命令。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我知道 PowerShell 有一个 -noexit
开关。
有没有办法在不使用该开关的情况下留在 shell 中?
换句话说,我想要一个执行然后让 shell 打开的脚本命令。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(10)
基本上,您有 3 个选项可以防止 PowerShell 控制台窗口关闭,我对此进行了描述 更多详细信息请参阅我的博客文章。
PowerShell -NoExit "C:\SomeFolder\SomeScript.ps1"
Read-Host -Prompt“Press Enter to exit”
有关要修改哪些注册表项的更多信息,请参阅我的博客。
听起来您正在寻找选项#1 或#3。
You basically have 3 options to prevent the PowerShell Console window from closing, that I describe in more detail on my blog post.
PowerShell -NoExit "C:\SomeFolder\SomeScript.ps1"
Read-Host -Prompt "Press Enter to exit"
See my blog for more information on which registry keys to modify.
Sounds like you are looking for option #1 or #3.
如果您不带参数运行该脚本,则该脚本将不会退出,例如双击它:
This script will not exit if you run it without arguments, e.g. by double-clicking on it:
您是否尝试过
这将停止执行并将它们放入嵌套提示符中。当他们退出该提示时,脚本将完成并且窗口将关闭。
Have you tried
That will stop execution and drop them to a nested prompt. When they exit that prompt, then the script will finish and the window will close.
是的,就像命令行一样——输出:
Yes, just like commandline -- output:
我不知道您可以在脚本中运行一个命令,如果未使用
-noexit
命令调用 shell,该命令会阻止 shell 退出。如果我不想关闭 shell,我通常会在最后使用
Read-Host "Press ENTER to continue"
。但是,如果 shell 不是使用-noexit
启动的,则这不会阻止 shell 在您按 Enter 后关闭。I'm not aware of a command you could run in the script that would prevent the shell from exiting if it had not been invoked using the
-noexit
command.I typically use
Read-Host "Press ENTER to continue"
at the end if I don't want the shell to close. However this won't prevent the shell from closing after you press enter if it had not been started with-noexit
.这个简单脚本末尾的 while 循环提示我输入命令并执行它。它在脚本的环境中运行,因此可以检查变量的值。执行时输入“exit”将终止循环。
我确信其他人能够分享一些改进,但这只是一个开始。问候。
The while loop at the end of this trivial script prompts me for a command and executes it. It runs with the environment of the script, so it is possible to check the values of variables. Entering "exit" terminates the loop when it is executed.
I'm sure that others will be able to share some refinements, but this is a start. Regards.
像这样?
Like this?
另一种方法是使用
$null = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
。结合其他答案,有以下几种选择:
PowerShell -NoExit "Path\to\Script\script.ps1"
暂停
read-Host -Prompt "Press Enter to exit"
$host.enternestedprompt()
$null = host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Another way is to use
$null = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
.Combining with other answers there are a few options:
PowerShell -NoExit "Path\to\Script\script.ps1"
pause
read-Host -Prompt "Press Enter to exit"
$host.enternestedprompt()
$null = host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
并不是要恢复 6 年前的线程或任何东西,但任何阅读的人都应该注意,
如果您启用了注册表调整(全局修复)并且实际上想要运行自动关闭脚本,则可以结束脚本。
Not to revive a 6-year-old thread or anything, but it should be noted to anyone reading that you can end your script with
if you have the registry tweak (global fix) enabled and actually want to run an automatically-closing script.