你好,我一直在制作一个小的跨平台脚本,我可以将其卷曲并通过管道传输到bash和Powershell中。基本思想是服务器向解释器发送一个命令,然后它给出一个命令将所有输出重定向到标准输出。 bash 中的一个例子是
#some commands
aplay rick.wav
cat -
random text
that will be redirected to stdout by cat...
bash will never see this
然后我将其通过管道传输到 bash 的标准输入
但对于 Powershell 我可以做cat test.ps1 | iex
or cat test.ps1 | powershell -
但无法在一个命令中连续将标准输入重定向到标准输出,例如cat -
因为 cat 不会从标准输入中查找。
还有一些旁注,在尝试了很多随机的事情之后,Windows 似乎有很多标准输入类型,一个是键盘,另一个是管道
您可以通过管道将文本行传输到powershell.exe
、Windows PowerShellCLI https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_powershell_exe, via -Command -
(-c -
),它会一一解释它们。
这是一个互动演示来自inside电源外壳;它与从外部通过管道输入(通过 stdin 提供)的工作方式相同:
# Repeatedly prompt for a line of input and execute it as a PowerShell command.
# Press Ctrl-C to exit.
& { while ($true) { Read-Host } } | powershell -noprofile -c -
Note:
-
-Command -
has problematic aspects, notably with commands that span multiple lines (an additional Enter keystroke / newline is then needed for the command to be recognized) and so does -File -
, whose behavior is even stranger - see this answer https://stackoverflow.com/a/42475326/45375 and GitHub issue #3223 https://github.com/PowerShell/PowerShell/issues/3223.
另一个演示,模拟outside标准输入通过2条管道输送到powershell -c -
:
'get-date', 'get-item /' | powershell -noprofile -c -
执行这两个命令并打印它们的输出;powershell.exe
然后退出,因为没有更多的 stdin 输入可用;然而,对于不定的标准输入输入(类似于cat -
在类 Unix 平台上)PowerShell 进程也将无限期地保持活动状态。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)