Powershell 3.0 - Workflows - Limit number of parallel execution

后端 未结 3 2003
故里飘歌
故里飘歌 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:53

    Just wanted to add this detail, ThrottleLimit switch mentioned above is available in Powershell v4.0, its not available in v3.0. We have a mix of 2.0 and 3.0 servers

    0 讨论(0)
  • 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 ..
        } 
      }
    }
    
    0 讨论(0)
  • 2021-02-20 16:10

    There is an option to limit the number of parallel processes in a foreach-parallel loop using -throttlelimit N. It's great for reducing the parallelism, but if you try a high number the system may still limit you to 5, depending on all your software versions (YAY! Microsoft consistency). I know the question is old, but since it came up on Google without a decent answer, I thought I'd chime in.

    Workflow Create-VM {
      $List = 1..500
      foreach -parallel -throttlelimit 4 ($Elem in $List)
      {
          # Create VM ...
          # Configure created VM ..
      }
    }
    
    Create-VM
    
    0 讨论(0)
提交回复
热议问题