I have a list of API requests I need to make, and I'm trying to speed it up using 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 $_))
}
However, Request-Something
calls a number of other custom functions from the same module, APIHelpers.psm1
I need a way to add all the necessary functions into that script block
I was considering building a hashtable that maps function names to function text, but I can't figure out the syntax for passing a variable to $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
}
Once I have this hashtable built, I imagine I could modify the original code:
$requests | ForEach-Object -parallel {
foreach ($funcName in $using:funcDefs.keys) {
${function:$funcName} = $using:funcDefs[$funcName]
}
$null = ($using:results).add((Request-Something -request $_))
}