Can Powershell Loop Through a Collection N objects at a time?

后端 未结 4 1455
感情败类
感情败类 2020-12-16 08:10

I am wondering how one would tackle looping through a collection of objects, processing the elements of that collection in groups instead of singularly, as is the case in no

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-16 08:42

    You guys definitely gave me some great ideas for this functionality. I ended up going with the following:

    #create base collection
    $group = get-vm
    $i = 0
    
    do {
        new-variable -Name "subgroup$i" -value $group[0..4]
        ++$i
        $group = $group[5..$group.length]
    }
    while ($group.length -gt 0)
    

    This code results in a number of subgroups, which is based on how many times the base collection is divisible by 5, which is the desired subgroup quantity in this case......

提交回复
热议问题