繁体   English   中英

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

[英]How to run powershell script (.ps1) in windows terminal powershell tab?

我尝试使用 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

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

它是命令 shell 的主机(powershell、bash、cmd、python、ZF83A0AA1F9CA79F7DD5994E048等)。 您必须告诉 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、ZF83A0AA1F9CA0F7EDD5994445BA7D,那么他们也需要。

For everyone that wants to personalise their PowerShell terminal inside windows terminal or run a script when PowerShell is opened inside windows terminal, you must do the following:

您必须运行所需的脚本作为传递给 PowerShell 的参数。 换句话说,您使用 powershell.exe 作为运行脚本的媒介,而不是作为运行脚本的媒介。 这意味着一旦所选脚本完成执行,powershell.exe 也会停止,因为可执行文件的功能已传递给脚本。 为了防止这种情况,您必须将属性“ PowerShell -NoExit ”传递给参数。

例子:


%SystemRoot%\\System32\\WindowsPowerShell\\v1.0\\powershell.exe PowerShell -NoExit %SystemRoot%\\System32\\WindowsPowerShell\\v1.0\\PowerShell_Startup_Ascii_Art.ps1

一旦脚本完成执行,这将阻止 PowerShell 关闭。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM