Create objects in parallel in powershell workflow and add it to array
I need to create many objects in parallel inside a workflow and add all objects to the array. My code will be something like this
workflow sample {
$ans=@()
$arr=@(1,2,3)
foreach -parallel ($a in $arr){
$obj= New-Object System.Object
$obj | Add-Member -type NoteProperty -Name "Number" -Value $a
$workflow:ans += $obj
}
$ans
}
But the way out for this
PSComputerName PSSourceJobInstanceId
-------------- ---------------------
localhost 56295d88-4599-495a-ae64-00d129f7e21c
localhost 56295d88-4599-495a-ae64-00d129f7e21c
localhost 56295d88-4599-495a-ae64-00d129f7e21c
I need an array containing three objects. How to achieve this in this scenario
+3
source to share
1 answer
try like this:
workflow sample {
$ans=@()
$arr=@(1,2,3)
foreach -parallel ($a in $arr){
$obj= New-Object -type PSObject -Property @{
Number = $a
}
$workflow:ans += $obj
}
$ans
}
sample | select -Property Number
Add-member
doesn't work that well in workflow
probably due to object serialization / deserialization.
+4
source to share