繁体   English   中英

PS1 Start-Process 脚本错误 - 无法在管道中间运行文档

[英]PS1 Start-Process script error - Cant run document in the middle of pipeline

我有 1 个 PowerShell 脚本 in.psm1 形式,所以我可以有一个自定义 cmdlet。 问题是,我不知道自己在做什么,所以我只是找到了一篇文章。 我复制了这篇文章,我所做的唯一更改是将 cmdlet function 更改为“Start-Process”。 我还让它启动了 WT.exe(Windows 终端)并添加了在普通 ps1 文件中完美运行的 arguments。

我的代码:

    Function txtedit
    {
        Start-Process -FilePath 'wt.exe' -ArgumentList "-NoExit python C:\Users\hjdom\OneDrive\Documents\pyUtils\texteditor\main.py"
    }

Export-ModuleMember -Function 'txtedit'

错误 PowerShell 返回:

PS C:\Windows\System32\WindowsPowerShell\v1.0\Modules\txtedit> c:\Windows\System32\WindowsPowerShell\v1.0\Modules\txtedit\txtedit.psm1
Cannot run a document in the middle of a pipeline: C:\Windows\System32\WindowsPowerShell\v1.0\Modules\txtedit\txtedit.psm1.
    + CategoryInfo          : InvalidOperation: (C:\Windows\Syst...it\txtedit.psm1:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : CantActivateDocumentInPipeline
谢谢!

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,那么他们也需要。

暂无
暂无

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

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