In a ForEach-Object -Parallel{}, I am trying to call variables with varying names defined prior to the function. This requires $Using:Name
to pull in a variable from outside of the function.
However, when trying to use $Using:$Name
or $Using:(Get-Variable -Name $Name)
it provides an error: "Variable reference is not valid. ':' was not followed by a valid variable name character. Consider using ${} to delimit the name."
When trying to use ${Using:$Name}
, {}
gives the literal string and does not work.
Other variations trying to make it work either provide the same variable reference is not valid or that the variable is null.
Variations tried:
$Using:{(Get-Variable -Name $Name)}
$Using:(Get-Variable -Name $Name)
Get-Variable -Name "Using:$Name"
Get-Variable -Name Using:$Name
Code Example:
$0IndexA = @(45,51,57)
$1IndexA = @(1490,1901,1903)
0..1 | Foreach-Object -Parallel {
$ParallelNumber = $_ # Used for readability
For ($c = 0; $c -le (Get-Variable $ParallelNumber"IndexA").value.count; $c++){
Get-Variable -Name $ParallelNumber"IndexA"[$c]
}
}
Error: Cannot find a variable with the name '0IndexA'.
Trying with -scope 1 Error:"The scope number '1' exceeds the number of active scopes. (Parameter 'Scope') Actual value was 1."
You're looking for variable indirection, i.e. the ability to refer to a variable indirectly, via its name stored in another variable.
However, in the context of the
$using:
scope this is not supported: what follows:
must be a literal variable name.As Darin suggests, a workaround is to use a hashtable in the caller's scope that maps the input objects to the values to be used for them:
Note the need to enclose the
$using:
reference in(...)
in order to apply an index ([$_]
) to it.[1][1] Arguably, this shouldn't be necessary, but is as of PowerShell 7.3.9 - see GitHub issue #10876 for a discussion.