powershell - 在 PowerShell 脚本中将字符串导入 cmd 不起作用
问题描述
当我直接在我的 PowerShell 窗口中执行它时,我有以下工作调用:
$myexe = "C:\MyExe.exe"
"MyString" | & $myexe // works
Write-Output "MyString" | & $myexe // seems to work too
但是,当我在 PowerShell 脚本的函数中执行相同操作时,它不再起作用。该程序没有收到字符串......有什么想法吗?
解决方案
与 POSIX 兼容的 shell(例如 )不同bash
,PowerShell 不会自动将给定脚本或函数接收到的管道输入中继到您从该脚本或函数内部调用的命令。
为此,您必须使用自动$input
变量[1]:
例如:
function foo {
# Relay the pipeline input received by this function
# to an external program, using `cat` as an example.
$input | & /bin/cat -n # append $args to pass options through
}
'MyString' | foo
请注意,&
在这种情况下它是可选的,因为/bin/cat
它是一个不带引号的文字路径 - 请参阅此答案以了解 when &
is required。
输出(在类 Unix 平台上)是: 1 MyString
,表明cat
实用程序foo
通过其标准输入接收到函数自己的管道输入,这要归功于使用$input
.
如果没有$input
到的管道cat
,后者将根本不会收到标准输入输入(并且在cat
会阻塞的情况下,等待交互式输入)。
如果您也想支持传递文件名参数- 代替管道输入 - 需要做更多的工作:
function foo {
if ($MyInvocation.ExpectingInput) { # Pipeline input present
# Relay the pipeline input received by this function
# to an external program, using `cat` as an example.
$input | /bin/cat -n $args
} else { # NO pipeline input.
/bin/cat -n $args
}
}
'MyString' | foo # pipeline input
foo file.txt # filename argument
注意:
只有非高级函数和脚本才能使用该变量,
$input
并且它的使用意味着通过管道传输到封闭函数/脚本的所有输入都在开始之前完全收集,在前面发送。要将脚本/函数自己的管道输入真正流式传输到外部程序的单个调用- 即在脚本/函数接收输入时中继输入 -需要直接使用 .NET API
System.Diagnostics.ProcessStartInfo
,即System.Diagnostics.Process
.
[1] 同样,$input
需要通过 PowerShell 的CLI从 PowerShell外部访问通过管道传输到 PowerShell 命令的数据;例如,来自:bash
echo hi | pwsh -c '$input | ForEach-Object { "[{0}]" -f $_ }'
推荐阅读
- php - 如何取消设置 Laravel 块中的引用变量传递?
- vb.net - 代理授权标头在 HttpWebRequest 中不起作用,以防对 URL 的 Rest 请求
- oracle - 对任何子表行的更新将整个父表锁定在 oracle
- laravel - Moodle 自动登录插件 - 如何将用户引导至特定课程?
- android - RecycleView Kotlin 与 ArrayList 总是大小 = 0
- jestjs - jest@23.1.0 collectCoverageFrom 不选择根目录外的文件
- javascript - 为什么 2 位数字不适用于 Date.toLocaleTimeString?
- excel - 使用模式分割线
- html - 为什么进度 html 元素没有显示在电子邮件(gmail 或 hotmail)上,有什么方法可以显示它,为什么 Nunito google 字体在电子邮件上不起作用?
- youtube - 是否可以从 YouTube iframe 嵌入播放器中删除“标题”“稍后观看”和“分享”按钮