阻止 Powershell 退出

发布于 2025-01-07 03:20:37 字数 125 浏览 0 评论 0 原文

我知道 PowerShell 有一个 -noexit 开关。

有没有办法在不使用该开关的情况下留在 shell 中?

换句话说,我想要一个执行然后让 shell 打开的脚本命令。

I know that there is that little -noexit switch for PowerShell.

Is there anyway of staying in the shell without using that switch?

In other words, I want a script command(s) that executes then leaves the shell open.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

红颜悴 2025-01-14 03:20:37

基本上,您有 3 个选项可以防止 PowerShell 控制台窗口关闭,我对此进行了描述 更多详细信息请参阅我的博客文章

  1. 一次性修复:从 PowerShell 控制台运行脚本,或使用 -NoExit 开关启动 PowerShell 进程。例如 PowerShell -NoExit "C:\SomeFolder\SomeScript.ps1"
  2. 每个脚本修复: 在脚本文件末尾添加输入提示。例如 Read-Host -Prompt“Press Enter to exit”
  3. 全局修复:更改注册表项,以便在脚本运行完毕后始终保持 PowerShell 控制台窗口打开。

有关要修改哪些注册表项的更多信息,请参阅我的博客。

听起来您正在寻找选项#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.

  1. One-time Fix: Run your script from the PowerShell Console, or launch the PowerShell process using the -NoExit switch. e.g. PowerShell -NoExit "C:\SomeFolder\SomeScript.ps1"
  2. Per-script Fix: Add a prompt for input to the end of your script file. e.g. Read-Host -Prompt "Press Enter to exit"
  3. Global Fix: Change your registry key to always leave the PowerShell Console window open after the script finishes running.

See my blog for more information on which registry keys to modify.

Sounds like you are looking for option #1 or #3.

无风消散 2025-01-14 03:20:37

如果您不带参数运行该脚本,则该脚本将不会退出,例如双击它:

param($Work)

# restart PowerShell with -noexit, the same script, and 1
if (!$Work) {
    powershell -noexit -file $MyInvocation.MyCommand.Path 1
    return
}

# now the script does something
# this script just outputs this:
'I am not exiting'

This script will not exit if you run it without arguments, e.g. by double-clicking on it:

param($Work)

# restart PowerShell with -noexit, the same script, and 1
if (!$Work) {
    powershell -noexit -file $MyInvocation.MyCommand.Path 1
    return
}

# now the script does something
# this script just outputs this:
'I am not exiting'
離人涙 2025-01-14 03:20:37

您是否尝试过

$host.enternestedprompt()

这将停止执行并将它们放入嵌套提示符中。当他们退出该提示时,脚本将完成并且窗口将关闭。

Have you tried

$host.enternestedprompt()

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.

提笔书几行 2025-01-14 03:20:37
"do stuff"
Pause

是的,就像命令行一样——输出:

do stuff
Press Enter to continue...:
"do stuff"
Pause

Yes, just like commandline -- output:

do stuff
Press Enter to continue...:
橘虞初梦 2025-01-14 03:20:37

我不知道您可以在脚本中运行一个命令,如果未使用 -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.

怂人 2025-01-14 03:20:37

这个简单脚本末尾的 while 循环提示我输入命令并执行它。它在脚本的环境中运行,因此可以检查变量的值。执行时输入“exit”将终止循环。

# Do something.

cd D:\temp
$current_directory = $(pwd)
dir

write-host  # Just add a little space after the dir

# Stay in the shell and execute commands.

while ($TRUE) {
  $cmd = Read-Host "PS $(pwd)"
  if ($cmd[0] -eq '"') { iex "& $cmd" } 
    else { iex $cmd }
}

我确信其他人能够分享一些改进,但这只是一个开始。问候。

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.

# Do something.

cd D:\temp
$current_directory = $(pwd)
dir

write-host  # Just add a little space after the dir

# Stay in the shell and execute commands.

while ($TRUE) {
  $cmd = Read-Host "PS $(pwd)"
  if ($cmd[0] -eq '"') { iex "& $cmd" } 
    else { iex $cmd }
}

I'm sure that others will be able to share some refinements, but this is a start. Regards.

痞味浪人 2025-01-14 03:20:37

像这样?

PowerShell -File  c:\myscript.ps1 -NoExit

Like this?

PowerShell -File  c:\myscript.ps1 -NoExit
笑忘罢 2025-01-14 03:20:37

另一种方法是使用 $null = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

结合其他答案,有以下几种选择:

    1. 从命令提示符运行: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:

    1. From command prompt run: PowerShell -NoExit "Path\to\Script\script.ps1"
    1. Add to end of script: pause
    1. Add to end of script: read-Host -Prompt "Press Enter to exit"
    1. Add to end of script: $host.enternestedprompt()
    1. Add to end of script: $null = host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
淑女气质 2025-01-14 03:20:37
Powershell -noexit -command {
$a=1
Echo $a
} # End block
# Script ends but PowerShell windows remains.
Powershell -noexit -command {
$a=1
Echo $a
} # End block
# Script ends but PowerShell windows remains.
完美的未来在梦里 2025-01-14 03:20:37

并不是要恢复 6 年前的线程或任何东西,但任何阅读的人都应该注意,

Stop-Process -processname regedit

如果您启用了注册表调整(全局修复)并且实际上想要运行自动关闭脚本,则可以结束脚本。

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

Stop-Process -processname regedit

if you have the registry tweak (global fix) enabled and actually want to run an automatically-closing script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文