Given a hashtable called $derivedValues
, and a variable that requires variable expansion, I am seeing some interesting performance issues.
Given:
$rule.opnd1 = 'uninstallString'
$rule.opnd2 = 'MsiExec.exe /X$($_.keyGUID)'
$filteredKeys.Where({$_.($rule.opnd1) -eq $ExecutionContext.InvokeCommand.ExpandString($rule.opnd2)})
I get good performance. A few thousandths of a second. But when the variables to be expanded aren’t $_
such as
$rule.opnd1 = 'displayName'
$rule.opnd2 = '$($derivedValues.displayName)'
performance is really bad. Easily approaching half a second. I am assuming it relates to referencing the $derivedValues
that is in the “parent” scope if you will, as compared to $_
which is defined within the scope of the closure by definition.
What I am wondering is, is there a way to actually pass the $derivedValues
variable into the closure so that the variable expansion is always local scope? And if so, might this improve performance? It’s a weird scenario I know, so if there is a way but no one knows if it will help because no one has ever needed to do such a thing, well, I’ll report back. 🙂