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

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

问题描述

我想知道如何处理通过对象集合的循环,以正常的Foreach循环中的情况处理集合中的元素而不是单独处理。例如,而不是:

  $ items = get-vm 
foreach($ item in $ items){做一些事情}

我想这样做:

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

基本上,这个语句打算说foreach中的5个项目做一些工作.....

<任何人都可以显示出完成这个所需的正确结构吗?

解决方案

b
$ b

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

($ i $($ i + = $ group) - 1)]
'*****'
}
直到($ i - ge $ array.count -1)


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 }

I would like to do this:

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

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?

解决方案

I've got 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天全站免登陆