运行PowerShell脚本后如何保持Shell窗口打开? [英] How to keep the shell window open after running a PowerShell script?

查看:1160
本文介绍了运行PowerShell脚本后如何保持Shell窗口打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a very short PowerShell script that connects to a server and imports the AD module. I'd like to run the script simply by double clicking, but I'm afraid the window immediately closes after the last line.

如何解决这个问题?

推荐答案

您基本上有3个选项可防止关闭PowerShell控制台窗口,我将其描述为在我的博客文章中详细介绍

You basically have 3 options to prevent the PowerShell Console window from closing, that I describe in more detail on my blog post.


  1. 一次性修复::从PowerShell控制台运行脚本,或使用-NoExit开关启动PowerShell进程。例如 PowerShell -NoExit C:\SomeFolder\SomeScript.ps1

  2. 按脚本修复:在脚本文件的末尾添加输入提示。例如读取主机-提示按Enter退出

  3. 全局修复:添加 -NoExit 开关以使脚本完成运行后始终保持PowerShell控制台窗口打开。

  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 by adding the -NoExit switch to always leave the PowerShell Console window open after the script finishes running.



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\""

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

See my blog for more information and a script to download that will make the registry change for you.

这篇关于运行PowerShell脚本后如何保持Shell窗口打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆