【发布时间】:2020-04-17 13:46:47
【问题描述】:
我找不到传递函数的方法。只是变量。
没有将函数放在 ForEach 循环中的任何想法?
function CustomFunction {
Param (
$A
)
Write-Host $A
}
$List = "Apple", "Banana", "Grape"
$List | ForEach-Object -Parallel {
Write-Host $using:CustomFunction $_
}
【问题讨论】:
-
要么将你的函数打包到一个模块中,要么(重新)定义它在
-Parallel块 -
顺便说一句:
Write-Hostis typically the wrong tool to use,除非意图是仅写入显示器,绕过成功输出流,并能够将输出发送到其他命令,将其捕获在变量中,将其重定向到文件。要输出一个值,请单独使用它;例如,$value而不是Write-Host $value(或使用Write-Output $value,尽管这很少需要)。另见:stackoverflow.com/a/50416448/45375的底部
标签: powershell powershell-core