【发布时间】:2021-07-27 15:15:45
【问题描述】:
我有一个我需要发出的 API 请求列表,我正在尝试使用 Foreach-Object -Parallel 来加速它:
$funcDef = ${function:Request-Something}.ToString()
$results = [System.Collection.ArrayList]@()
$requests | ForEach-Object -parallel {
${function:Request-Something} = $using:funcDef
$null = ($using:results).add((Request-Something -request $_))
}
但是,Request-Something 从同一模块 APIHelpers.psm1 调用许多其他自定义函数
我需要一种方法将所有必要的功能添加到该脚本块中
我正在考虑构建一个将函数名称映射到函数文本的哈希表,但我无法弄清楚将变量传递给 $function 的语法:
$funcDefs = @{}
(Get-Command -Module APIHelpers).Name | ForEach-Object {
$name = $_
$funcDefs[$name] = ${function:$`{name`}} #This does NOT work, I can't figure out the syntax
}
一旦我建立了这个哈希表,我想我可以修改原始代码:
$requests | ForEach-Object -parallel {
foreach ($funcName in $using:funcDefs.keys) {
${function:$funcName} = $using:funcDefs[$funcName]
}
$null = ($using:results).add((Request-Something -request $_))
}
【问题讨论】:
-
看看这个答案stackoverflow.com/questions/67393819/…可能对你有帮助