How can we add an item to an arraylist while using foreach -parallel
loop in powershell
workflows?
workflow foreachpsptest {
param([string[]]$list)
$newList = [System.Collections.ArrayList]@()
foreach –parallel ($item in $list){
$num = Get-Random
InlineScript {
($USING:newList).add("$num---$item")
}
}
Write-Output -InputObject "randomly created new List: $newList"
}
$list = @("asd","zxc","qwe","cnsn")
foreachpsptest -list $list
The problem here is that you're using, uh
$using:
the wrong way.Your workflow is basically it's own sandbox. You can use $using to instantiate a variable with the same values within it, but you can't use that to manipulate the same variable outside of it.
However, you can have your workflow emit an object, and then capture that with the .Add() method of your
$newlist
Arraylist variable.Tweak the code like so, and it should work:
Then, after the code has run, we can get the values out, like so.