通过.lnk文件将管道输送到可执行文件中
我在 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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CMD.EXE
不同, PowerShell确实 支持调用快捷键文件(.lnk
),例如 Console Applications ;反而:a new 控制台窗口打开,异步。
新控制台窗口中的过程未接收stdin输入(通过管道):
如果使用了命令 - 例如
echo hello
,使用内置echo
powershell的别名(很少需要)写入输出
cmdlet-以下错误发生,如Windows PowerShell v5.1/powerShell(core)7.2.4:无法在管道中间运行文档
如果表达式 - 例如
'Hello'
- 使用,则不会发生错误,但是输入有效地 。换句话说: powerShell认为
.lnk
files document ,而不是可执行文件,而对Windows(gui)shell进行辩护以打开它们; [1] 实际上,调用.lnk
文件就像将其传递到indoke> indoke> Invoke-Item
或 -Wait 使调用同步,但仍在单独的窗口中运行,并且不支持STDIN输入;尝试使用-nonewwindow
和/或-redirectStandardInput
(通过A file 提供stdin输入) ,类似于您看到的: [2]由于错误而无法运行此命令:%1不是有效的Win32应用程序。
解决方案:
.exe
file 直接在管道中,如果您的路径为,请引用 /em>和/或包含变量引用或表达式 - 需要使用&
,; 调用外部程序时,但是在上述情况下,仅是必需的,在此答案。
cmd/c
调用快捷方式(.lnk
)文件:请注意,在两个解决方法中, 字符编码问题可能会出现,鉴于数据被发送到外部程序:
存储在
[console] :: outputEncoding
中的编码如何确定PowerShell如何从外部程序中接收到的数据。参见此答案有关更多信息。
[1]请注意,(暂时)附加
“;。lnk”
to$ envy的值:pathext
,列出了所有属于的文件名扩展名的环境变量可执行文件,dis not help。从
Shellexecute
Winapi函数到createProcess
,后者仅与实际的可执行文件一起使用。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-inecho
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 toInvoke-Item
orStart-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:
.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: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..lnk
) file viacmd /c
: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 theShellExecute
WinAPI function toCreateProcess
, and the latter only works with actual executables.