简体   繁体   English

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

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

I have tried making the script open with windows terminal (wt.exe) but it displays the error:我尝试使用 windows 终端(wt.exe)打开脚本,但它显示错误:

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

I really need it to run in the windows terminal because it allows you to keep typing commands after the script is over.我真的需要它在 windows 终端中运行,因为它允许您在脚本结束后继续输入命令。 I am using the script to run a python file.我正在使用脚本运行 python 文件。

this is what the ps1 script contains:这是 ps1 脚本包含的内容:

python ./pythonfiles/startgame.py

Thanks so much!非常感谢! -BlueFalconHD -BlueFalconHD

The Windows Terminal (wt.exe) is not a command shell. Windows 终端 (wt.exe) 不是命令 shell。

It is a host for command shells (powershell, bash, cmd, python, perl, etc.).它是命令 shell 的主机(powershell、bash、cmd、python、ZF83A0AA1F9CA79F7DD5994E048等)。 You have to tell WT.exe which to use, to call your script.您必须告诉 WT.exe 使用哪个来调用您的脚本。

Example - cmd.exe or via the WinKey Run:示例 - cmd.exe 或通过 WinKey 运行:

Type:类型:

wt d:\temp\hello.ps1

this will fail with...这将失败...

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

... even though Windows Terminal launches with whatever default shell is in your WT settings. ...即使 Windows 终端在您的 WT 设置中使用任何默认 shell 启动。

Now do this...现在这样做...

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

... this works, because you told wt.exe which shell to use to execute your script. ...这行得通,因为您告诉 wt.exe 使用哪个 shell 来执行您的脚本。

Update as per your comment.根据您的评论更新。

but then how do I prevent the terminal from closing after the script is done running?但是在脚本运行完成后如何防止终端关闭?

Your target shell needs to provide a method for that.您的目标 shell 需要为此提供一种方法。 Powershell (Windows and Core) provide this via the -NoExit switch. 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

If you are calling another shell, bash, python, perl, etc., then they too would need to provide that.如果您正在调用另一个 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: 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:

You must run the desired script as an argument passed to PowerShell.您必须运行所需的脚本作为传递给 PowerShell 的参数。 In other words, you use the powershell.exe as an medium through which you run the script and not as the medium in which the script is run.换句话说,您使用 powershell.exe 作为运行脚本的媒介,而不是作为运行脚本的媒介。 This means that once the chosen script finished executing, powershell.exe will stop too because the executable's functionality was passed to the script.这意味着一旦所选脚本完成执行,powershell.exe 也会停止,因为可执行文件的功能已传递给脚本。 In order to prevent this you must pass to the argument the attribute " PowerShell -NoExit ".为了防止这种情况,您必须将属性“ PowerShell -NoExit ”传递给参数。

Example:例子:


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

This will prevent PowerShell from closing, once the script finished executing.一旦脚本完成执行,这将阻止 PowerShell 关闭。

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

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