Iterate through a growing array
$items = (1, 2, 3)
Write-Output $items
Write-Output ("size: {0}" -f $items.Count)
Write-Output ----
# For works well
$counter = 0
For ($i = 0; $i -lt $items.Count; $i++)
{
Write-Output ("i: {0}" -f $i)
Write-Output ("value: {0}" -f $items[$i])
$items = $items + ($counter)
$counter++
if($counter -gt 10) { break }
}
Write-Output --------------------------
$items = (1, 2, 3)
Write-Output $items
Write-Output ("size: {0}" -f $items.Count)
Write-Output ----
# ForEach does not work, it only loops over the inital size
$counter = 0
ForEach ($item in $items)
{
Write-Output ("counter: {0}" -f $counter)
Write-Output ("value: {0}" -f $item)
$items = $items + ($counter)
$counter++
if($counter -gt 10) { break }
}