如何在 Windows 终端 powershell 选项卡中运行 powershell 脚本 (.ps1)?

Blu*_*nHD 5 powershell windows-terminal

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

[error 0x800700c1 when launching `"C:\Users\hjdom\OneDrive\Desktop\All desktop stuff\Python Game\RunGame.ps1"']
Run Code Online (Sandbox Code Playgroud)

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

这是 ps1 脚本包含的内容:

python ./pythonfiles/startgame.py
Run Code Online (Sandbox Code Playgroud)

非常感谢!-蓝隼HD

pos*_*ote 10

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

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

示例 - cmd.exe 或通过 WinKey Run:

类型:

wt d:\temp\hello.ps1
Run Code Online (Sandbox Code Playgroud)

这将会失败...

[error 0x800700c1 when launching `d:\temp\hello.ps1']
Run Code Online (Sandbox Code Playgroud)

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

现在做这个...

wt.exe powershell D:\Scripts\hello.ps1
Run Code Online (Sandbox Code Playgroud)

...这有效,因为您告诉 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
Run Code Online (Sandbox Code Playgroud)

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