【问题标题】:Stop Powershell from exiting阻止 Powershell 退出
【发布时间】:2012-03-10 21:18:10
【问题描述】:

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

有没有在不使用那个开关的情况下留在 shell 中?

换句话说,我想要一个脚本命令,它执行然后让 shell 保持打开状态。

【问题讨论】:

  • 要(甚至)更具体:用户右键单击脚本,从上下文菜单中选择“在 Powershell 中运行”,脚本执行并保持外壳打开。
  • 你需要更改上下文菜单午餐添加-noexit
  • 克里斯蒂安,这显然不是来自脚本。

标签: powershell powershell-2.0 powershell-remoting


【解决方案1】:

您基本上有 3 个选项来阻止 PowerShell 控制台窗口关闭,我描述了 in more detail on my blog post

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

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

听起来您正在寻找选项 #1 或 #3。

【讨论】:

    【解决方案2】:

    如果您在没有参数的情况下运行此脚本,例如,该脚本将不会退出。双击它:

    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'
    

    【讨论】:

    • 我想发布这个黑客。与 UAC 海拔 hack 相同的技术。不漂亮,但很管用。
    【解决方案3】:

    你试过了吗

    $host.enternestedprompt()
    

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

    【讨论】:

    • 我刚刚注意到这个问题也被标记为 powershell-remoting。如果有问题的脚本在远程系统上作为作业运行,则该 hack 将不起作用。
    【解决方案4】:
    "do stuff"
    Pause
    

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

    do stuff
    Press Enter to continue...:
    

    【讨论】:

    • 那么用户之后应该如何使用powershell-window来输入命令呢?
    【解决方案5】:

    我不知道如果没有使用-noexit 命令调用,您可以在脚本中运行的命令会阻止 shell 退出。

    如果我不希望 shell 关闭,我通常在最后使用 Read-Host "Press ENTER to continue"。但是,如果 shell 不是以 -noexit 启动的,这不会阻止 shell 在您按 enter 后关闭。

    【讨论】:

    • 所以,不幸的是,这不是我需要的。
    • noexit 改变了 PowerShell.exe 的初始化方式,类似于cmd.exe /k。一旦它以一种方式被初始化,我认为它不能被改变。
    【解决方案6】:

    这个简单脚本末尾的 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 }
    }
    

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

    【讨论】:

      【解决方案7】:

      像这样?

      PowerShell -File  c:\myscript.ps1 -NoExit
      

      【讨论】:

      • 不,如果我单击包含您的行的文件,它仍然会退出 PS。我需要一种从脚本中阻止当前会话退出的方法。
      【解决方案8】:

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

      结合其他答案有几个选项:

        1. 从命令提示符运行:PowerShell -NoExit "Path\to\Script\script.ps1"
        1. 添加到脚本末尾:pause
        1. 添加到脚本末尾:read-Host -Prompt "Press Enter to exit"
        1. 添加到脚本末尾:$host.enternestedprompt()
        1. 添加到脚本末尾:$null = host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

      【讨论】:

        【解决方案9】:
        Powershell -noexit -command {
        $a=1
        Echo $a
        } # End block
        # Script ends but PowerShell windows remains.
        

        【讨论】:

        • 这正是 OP 所说的不想要的。 [咧嘴]
        【解决方案10】:

        不要恢复一个 6 年前的线程或任何东西,但应该向任何阅读的人注意,你可以用

        结束你的脚本
        Stop-Process -processname regedit
        

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

        【讨论】:

          猜你喜欢
          • 2012-04-16
          • 1970-01-01
          • 1970-01-01
          • 2014-02-07
          • 2021-08-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多