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



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

[error 0x800700c1 when launching `"C:UsershjdomOneDriveDesktopAll desktop stuffPython GameRunGame.ps1"']

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

这就是ps1脚本包含的内容:

python ./pythonfiles/startgame.py

非常感谢!-BlueFalconHD

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

它是命令shell(powershell、bash、cmd、python、perl等(的主机。你必须告诉WT.exe要使用哪个,才能调用你的脚本。

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

类型:

wt d:temphello.ps1

这将失败。。。

[error 0x800700c1 when launching `d:temphello.ps1']

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

现在做这个。。。

wt.exe powershell D:Scriptshello.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:Scriptshello.ps1
wt.exe pwsh -NoProfile -NoLogo -NoExit D:Scriptshello.ps1

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

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

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

示例:


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

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

最新更新