通过.lnk文件将管道输送到可执行文件中

发布于 2025-02-04 02:03:00 字数 2266 浏览 3 评论 0 原文

我在 c:\ youry \ long \ path \ streamtoclipboard.exe
上有一个可执行文件 文件 c:\ inpath \ streamToclipboard.lnk 指向该可执行文件。
目录 c:\ inpath 在我的路径变量中, .lnk 在我的Pathext变量中。

在常规CMD中,我可以执行任何以下命令:
Echo Hello | c:\ youry \ long \ path \ streamtoclipboard.exe
Echo Hello | C:\ Inpath \ streamToclipboard.lnk
Echo Hello | StreamToclipboard.lnk
Echo Hello | StreamToclipboard
并且可以启动可执行文件,文本“ Hello”已正确地输入到该过程中。

在PowerShell中,我可以执行 echo Hello | c:\ youmy \ long \ path \ streamToclipboard.exe ,它也起作用。但是所有其他命令都无法使用:(

Fehler beim Ausführen des Programms "StreamToClipboard.lnk": Die angegebene ausführbare Datei ist keine gültige
Anwendung für diese Betriebssystemplattform.In Zeile:1 Zeichen:12
+ echo foo | C:\InPath\StreamToClipboard.lnk
+            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.
In Zeile:1 Zeichen:1
+ echo foo | C:\InPath\StreamToClipboard.lnk
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (:) [], ApplicationFailedException
    + FullyQualifiedErrorId : NativeCommandFailed

和各自的路径)
在执行“ StreamToclipboard.lnk”时,大致转化为错误:指定的可执行文件不是此操作系统Plattform 的有效应用程序。

请注意, Echo Hello | “ c:\ youmy \ long \ path \ streamtoclipboard.exe” 也不起作用,带有不同的错误消息:

In Zeile:1 Zeichen:14
+ ... cho Hello | "C:\Very\Long\Path\StreamToClipboard.exe"
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ausdrücke sind nur als erstes Element einer Pipeline zulässig.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ExpressionsMustBeFirstInPipeline

大致转换为表达式仅有效即可作为管道中的第一个元素> 。

如果我,而不是创建.lnk文件,而是将整个可执行文件复制到 c:\ inpath \ intpath \ streamToclipboard.exe ,那么这些命令:
Echo Hello | C:\ Inpath \ streamToclipboard.exe
Echo Hello | streamToclipboard.exe
Echo Hello | StreamToclipboard
工作正常。

我如何让PowerShell接受 Echo Hello | StreamToclipboard (其中是.lnk文件)或至少 echo hello | StreamToclipboard.lnk

I have an executable at C:\Very\Long\Path\StreamToClipboard.exe
The file C:\InPath\StreamToClipboard.lnk points to that executable.
The directory C:\InPath is in my PATH variable, and .lnk is in my PATHEXT variable.

In regular cmd, I can execute any of these commands:
echo Hello | C:\Very\Long\Path\StreamToClipboard.exe
echo Hello | C:\InPath\StreamToClipboard.lnk
echo Hello | StreamToClipboard.lnk
echo Hello | StreamToClipboard
and the the executable is started, the text "Hello" is correctly piped into that process.

In PowerShell, I can execute echo Hello | C:\Very\Long\Path\StreamToClipboard.exe and it works too. But all of the other commands don't work:

Fehler beim Ausführen des Programms "StreamToClipboard.lnk": Die angegebene ausführbare Datei ist keine gültige
Anwendung für diese Betriebssystemplattform.In Zeile:1 Zeichen:12
+ echo foo | C:\InPath\StreamToClipboard.lnk
+            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.
In Zeile:1 Zeichen:1
+ echo foo | C:\InPath\StreamToClipboard.lnk
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (:) [], ApplicationFailedException
    + FullyQualifiedErrorId : NativeCommandFailed

(and respective paths)
Which roughly translates to Error while executing "StreamToClipboard.lnk": The specified executable file is not a valid application for this operating system plattform.

Note that echo Hello | "C:\Very\Long\Path\StreamToClipboard.exe" also does not work, with a different error message:

In Zeile:1 Zeichen:14
+ ... cho Hello | "C:\Very\Long\Path\StreamToClipboard.exe"
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ausdrücke sind nur als erstes Element einer Pipeline zulässig.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ExpressionsMustBeFirstInPipeline

Roughly translates to Expressions are only valid as the first element in a pipeline.

If I, instead of creating the .lnk file, copy the entire executable to C:\InPath\StreamToClipboard.exe, then these commands:
echo Hello | C:\InPath\StreamToClipboard.exe
echo Hello | StreamToClipboard.exe
echo Hello | StreamToClipboard
work fine.

How can I get PowerShell to accept echo Hello | StreamToClipboard (where it's an .lnk file), or at least echo Hello | StreamToClipboard.lnk?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风柔一江水 2025-02-11 02:03:00

解决方案

  • 呼叫目标 .exe file 直接在管道中,如果您的路径为,请引用 /em>和/或包含变量引用或表达式 - 需要使用&
# & is required, because the executable path is quoted.
# Note: 'Hello' is the PowerShell-idiomatic equivalent of
#        echo Hello
'Hello' | & "C:\Very\Long\Path\StreamToClipboard.exe"

​; 调用外部程序时,但是在上述情况下,仅是必需的,在此答案

  • 通过 cmd/c 调用快捷方式( .lnk )文件:
'Hello' | cmd /c C:\InPath\StreamToClipboard.lnk

请注意,在两个解决方法中, 字符编码问题可能会出现,鉴于数据被发送到外部程序


  • 存储在 [console] :: outputEncoding 中的编码如何确定PowerShell如何从外部程序中接收到的数据。

  • 参见此答案有关更多信息。


[1]请注意,(暂时)附加“;。lnk” to $ envy的值:pathext ,列出了所有属于的文件名扩展名的环境变量可执行文件,dis not help

。从 Shellexecute Winapi函数到 createProcess ,后者仅与实际的可执行文件一起使用。

  • Unlike cmd.exe, PowerShell does not support invoking shortcut files (.lnk) like console applications; instead:
    • A new console window opens, asynchronously.

    • The process in the new console window does NOT receive stdin input (via the pipeline):

      • If a command is used - such as echo hello, using the built-in echo alias for PowerShell's (rarely needed) Write-Output cmdlet - the following error occurs, as of Windows PowerShell v5.1 / PowerShell (Core) 7.2.4:

        • Cannot run a document in the middle of a pipeline
      • If an expression - such as 'hello' - is used, no error occurs, but the input is effectively ignored.

    • In other words: PowerShell considers .lnk files documents, not executables, and defers to Windows (GUI) shell for opening them;[1] in effect, invoking a .lnk file is like passing it to Invoke-Item or Start-Process; adding -Wait to the latter makes the invocation synchronous, but still runs in a separate window and doesn't support stdin input; attempting to use -NoNewWindow and/or
      -RedirectStandardInput (to provide stdin input via a file) results in an error, similar to the one you saw:[2]

      • This command cannot be run due to the error: %1 is not a valid Win32 application.

Workarounds:

  • Call the target .exe file directly in your pipeline, which - if your path is quoted and/or contains variable references or expressions - requires use of &, the call operator:
# & is required, because the executable path is quoted.
# Note: 'Hello' is the PowerShell-idiomatic equivalent of
#        echo Hello
'Hello' | & "C:\Very\Long\Path\StreamToClipboard.exe"

Note: For simplicity, you may choose to always use & when invoking external programs, but it is only required in the cases mentioned above, discussed in more detail in this answer.

  • Invoke the shortcut (.lnk) file via cmd /c:
'Hello' | cmd /c C:\InPath\StreamToClipboard.lnk

Note that, in both workarounds, character encoding issues may arise, given that data is being sent to an external program:

  • The $OutputEncoding preference variable controls what encoding is used to send data to an external program.

  • The encoding stored in [Console]::OutputEncoding determines how PowerShell decodes data received from external programs.

  • See this answer for more information.


[1] Note that (temporarily) appending ";.LNK" to the value of $env:PATHEXT, the environment variable that lists all filename extensions that belong to executables, does not help.

[2] The reason is that these parameters cause Start-Process to switch from the ShellExecute WinAPI function to CreateProcess, and the latter only works with actual executables.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文