Powershell 可以一次循环遍历一个集合 N 个对象吗? [英] Can Powershell Loop Through a Collection N objects at a time?

查看:20
本文介绍了Powershell 可以一次循环遍历一个集合 N 个对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何处理对象集合的循环,以组的形式处理该集合的元素,而不是像普通 Foreach 循环那样单独处理.例如,而不是这样:

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 normal Foreach loops. For example, instead of this:

$items = get-vm
foreach ($item in $items) { do something }

我想这样做:

$items = get-vm
foreach ((5)$item in $items) {do something}

本质上,这个语句的目的是说 items 中的 foreach 5 items do some work.....

Essentially, this statement intends to say foreach 5 items in items do some work.....

谁能告诉我完成此操作所需的正确构造?

Can anyone show me the proper constructs required to accomplish this?

推荐答案

我有这个:

 $array = 1..100
 $group = 10
 $i = 0

 do {
     $array[$i..(($i+= $group) - 1)]
     '*****'
     }
      until ($i -ge $array.count -1)

这篇关于Powershell 可以一次循环遍历一个集合 N 个对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆