Powershell 3.0 - Workflows - Limit number of parallel execution

后端 未结 3 2020
故里飘歌
故里飘歌 2021-02-20 15:40

I am cloning VMs on ESX server from template. Simplified code looks like this:

Workflow Create-VM {
  $List = 1..500
  foreach -parallel ($Elem in $List)
  {
            


        
3条回答
  •  一个人的身影
    2021-02-20 15:54

    A trivial solution is to divide the list into smaller chunks and use that as input for parallel foreach. Like so,

    Workflow Create-VM {
      $List = 1..500
      # Calclulate indexes to get elements 0,3; 4,7; 8,11 and so on
      # Use the .. operator to extract those elements from $list and pass
      # 'em to foreach -parallel processing part
      for($i=0;$i -le $List.Count-4; $i+=4) { 
        foreach -parallel ($Elem in $list[$i..($i+3)]) {
          # Create VM ...
          # Configure created VM ..
        } 
      }
    }
    

提交回复
热议问题