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

问题描述 投票:0回答:3

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

powershell windows-terminal
3个回答
10
投票

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

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

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

类型:

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 等,那么它们也需要提供这些。


1
投票

对于想要在 Windows 终端内个性化其 PowerShell 终端或在 Windows 终端内打开 PowerShell 时运行脚本的每个人,您必须执行以下操作:

您必须运行所需的脚本作为传递给 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 关闭。


0
投票

要确定

.ps1
文件在
WT.exe
中运行或不使用
(GPS).ProcessName
。如果
WindowsTerminal
存在,则意味着它在
wt.exe
上运行。

如果安装了

wt.exe
。此脚本将始终从
.ps1
wt.exe
运行
pwsh
中的
powershell
文件。

$isWT = where.exe wt.exe 2>$null
$PS7  = $Host.Version.Major -eq 7

if ( !$isWT ) {'WT not Installed yet'; pause; exit 0}
if ( 'WindowsTerminal' -notin (GPS).ProcessName ) {
    if ( $PS7 ) {wt.exe PWSH $PSCommandPath; exit 0}
    $AL = "-d `"$PSScriptRoot`" -p `"Windows PowerShell`"",
          "PowerShell -NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`""
    start wt.exe -ArgumentList $AL ; exit 0
}

$PyFile = 'some .py file path'
if ( Test-Path $PyFile ) { python $PyFile }
pause 
© www.soinside.com 2019 - 2024. All rights reserved.