【问题标题】:How to keep the shell window open after running a PowerShell script?运行 PowerShell 脚本后如何保持 shell 窗口打开?
【发布时间】:2013-05-20 07:29:10
【问题描述】:

我有一个非常短的 PowerShell 脚本,它连接到服务器并导入 AD 模块。我想通过双击运行脚本,但恐怕窗口会在最后一行之后立即关闭。

我该如何解决这个问题?

【问题讨论】:

标签: active-directory powershell-2.0


【解决方案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. 全局修复:通过添加 -NoExit 开关来更改您的注册表项,以便在脚本完成运行后始终保持 PowerShell 控制台窗口打开。
Registry Key: HKEY_CLASSES_ROOT\Applications\powershell.exe\shell\open\command
Description: Key used when you right-click a .ps1 file and choose Open With -> Windows PowerShell.
Default Value: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "%1"
Desired Value: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "& \"%1\""

Registry Key: HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\0\Command
Description: Key used when you right-click a .ps1 file and choose Run with PowerShell (shows up depending on which Windows OS and Updates you have installed).
Default Value: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & '%1'"
Desired Value: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoExit "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & \"%1\""

请参阅我的博客以获取更多信息和下载脚本,该脚本将为您更改注册表。

【讨论】:

  • OPs 答案也可以作为每个脚本修复:使用 powershell -NoExit -Command { <The script to actually run> } 包装您的脚本 使用户能够通过 UI 启动脚本并查看结果而无需编辑注册表项
【解决方案2】:

错误... 我应该知道的:

powershell -noexit <path\script> 

仅此而已:)

【讨论】:

  • @IgorGanapolsky 问题可能是您在脚本路径后附加 -NoExit ,它需要在前面。我想如果它是在将它作为参数传递给脚本而不是 powershell.exe 之后。
【解决方案3】:

以下解决方案在运行 Powershell ISE 时阻止脚本关闭,否则允许脚本关闭。

# If running in the console, wait for input before closing.
if ($Host.Name -eq "ConsoleHost")
{
    Write-Host "Press any key to continue..."
    $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp") > $null
}

【讨论】:

    【解决方案4】:

    在我自己的情况下,我想将 powershell 添加到 Windows 7 上的上下文菜单中。即右键单击文件夹或文件夹内以获取菜单以启动 Powershell 窗口,而不会在启动后关闭它。这里的答案帮助我做到了这一点,我想在这里分享它,以防它帮助其他人。

    • 按 WIN + R 启动注册表编辑器 输入 regedit.exe 并回车
    • 导航到 HKEY_CLASSES_ROOT\Directory\Background\shell
    • 右键单击 shell 并创建一个键并为其命名,例如 PowershellHere
    • 在右侧窗格中,双击默认并提供描述性名称,例如 Powershell Here
    • 右键单击您之前创建的 PowershellHere 键并创建一个新键并将其命名为“command”,请确保正确命名,但不要使用引号。
    • 在右侧窗格中,双击默认,然后键入以下命令
    • C:\Windows\system32\WindowsPowerShell\v1.0\PowerShell.exe -noexit -Command CD '"%1"' -noexit 标志确保 Powershell 窗口不会在启动后立即再次关闭 '"%1"' 标志代表您右键单击的文件夹 -Command CD '"%1"' 将确保 Powershell 更改为右键单击的目录。

    要使右键单击在文件夹内工作,这意味着右键单击文件夹内的空白区域,请重复这些步骤,但这一次,注册表位置是:

    HKEY_CLASSES_ROOT\Directory\shell
    命令是:
    C:\Windows\system32\WindowsPowerShell\v1.0\PowerShell .exe -noexit

    在 Windows 7 Ultimate sp1 上测试过,但我想我可能也适用于更高版本的 Windows

    【讨论】:

      【解决方案5】:

      只需在脚本底部的新行添加暂停,就像在批处理文件中一样。

      【讨论】:

        【解决方案6】:

        如果您只想让窗口保持打开状态,可以在脚本末尾添加pause,或者如果您希望之后能够运行命令,可以添加powershell(显然不要执行第二个如果其他人将使用您的代码,则可以选择)。

        【讨论】:

          猜你喜欢
          • 2016-06-29
          • 1970-01-01
          • 1970-01-01
          • 2010-11-03
          • 2012-03-03
          • 2013-01-29
          • 1970-01-01
          • 2013-02-07
          相关资源
          最近更新 更多