【问题标题】:How to run powershell script (.ps1) in windows terminal powershell tab?如何在 Windows 终端 powershell 选项卡中运行 powershell 脚本(.ps1)?
【发布时间】:2021-06-30 07:51:47
【问题描述】:

我尝试使用 Windows 终端 (wt.exe) 打开脚本,但它显示错误:

[error 0x800700c1 when launching `"C:\Users\hjdom\OneDrive\Desktop\All desktop stuff\Python Game\RunGame.ps1"']

我真的需要它在 Windows 终端中运行,因为它允许您在脚本结束后继续输入命令。我正在使用脚本运行 python 文件。

这是 ps1 脚本包含的内容:

python ./pythonfiles/startgame.py

非常感谢! -BlueFalconHD

【问题讨论】:

  • 请编辑您的问题以显示完整的wt.exe 命令行并说明您在哪里调用它来自

标签: powershell windows-terminal


【解决方案1】:

Windows 终端 (wt.exe) 不是命令外壳。

它是命令 shell(powershell、bash、cmd、python、perl 等)的主机。您必须告诉 WT.exe 使用哪个来调用您的脚本。

示例 - cmd.exe 或通过 WinKey 运行:

类型:

wt d:\temp\hello.ps1

这将失败并...

[error 0x800700c1 when launching `d:\temp\hello.ps1']

...即使 Windows 终端使用 WT 设置中的任何默认 shell 启动。

现在执行此操作...

wt.exe powershell D:\Scripts\hello.ps1

...这行得通,因为您告诉 wt.exe 使用哪个 shell 来执行您的脚本。

根据您的评论进行更新。

但是如何防止终端在脚本之后关闭 跑完了吗?

你的目标 shell 需要为此提供一个方法。 Powershell(Windows 和 Core)通过 -NoExit 开关提供此功能。

powershell /?

PowerShell[.exe] [-PSConsoleFile <file> | -Version <version>]
    [-NoLogo] [-NoExit] [-Sta] [-Mta] [-NoProfile] [-NonInteractive]
    [-InputFormat {Text | XML}] [-OutputFormat {Text | XML}]
    [-WindowStyle <style>] [-EncodedCommand <Base64EncodedCommand>]
    [-ConfigurationName <string>]
    [-File <filePath> <args>] [-ExecutionPolicy <ExecutionPolicy>]
    [-Command { - | <script-block> [-args <arg-array>]
                  | <string> [<CommandParameters>] } ]

wt.exe powershell -NoProfile -NoLogo -NoExit D:\Scripts\hello.ps1

wt.exe pwsh -NoProfile -NoLogo -NoExit D:\Scripts\hello.ps1

如果您正在调用另一个 shell、bash、python、perl 等,那么它们也需要提供。

【讨论】:

  • 但是在脚本运行完成后如何防止终端关闭?我希望你能够输入命令。
猜你喜欢
  • 1970-01-01
  • 2021-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-03
  • 1970-01-01
  • 2011-10-12
  • 1970-01-01
相关资源
最近更新 更多