2017-04-10 98 views
6

我想觸發一個PowerShell工作流程,它應該同時旋轉10個線程。我正在使用PS版本4.代碼 -如何增加虛擬機上的cuncurrent powershell實例數量?

Workflow CreateVMs 
{ 
    $i = 0 
    #somecodehere... 
    foreach -parallel -throttlelimit 10($i in 0..30) 
    { 
     #somemorecodehere... 
     # INVOKING BATCH FILE USING POWERSHELL 
    } 
} 

我在我的內聯腳本中使用powershell調用批處理文件。我在任務管理器觀察到的是,只有5個線程是活躍在時間

enter image description here

可能成爲下一個線程獲得的回升只是五個一工程已完成。我從來沒有看到超過5 ps的實例。然而,當我檢查每個用戶允許的PS會話是遠遠超過5

enter image description here

我怎樣才能在一個PS工作流程分拆PS的10個線程並行。 我在這裏錯過了什麼?

+1

只要並行活動的數量未超過節流限制,Workflow Foundation將自行管理線程/活動池,您無法強制其「加快速度」 –

+0

在我的情況下它甚至沒有達到油門限制。 – Atf

+0

不確定這是否適用於您,但您是否考慮過使用Start-Job cmdlet來完成您正在嘗試執行的操作? https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.core/start-job –

回答

1

考慮下面的示例腳本:

Workflow Test-Workflow 
{ 
    foreach -Parallel -ThrottleLimit 10 ($Number in 1..20) 
    { 
     $RandomSeconds = Get-Random -Minimum 10 -Maximum 60 

     InlineScript 
     { 
      $String = '{0:s}: Starting number {1:D2} for {2:D2} seconds' -f (Get-Date),$Using:Number,$Using:RandomSeconds 
      Write-Host -Object $String 
     } 

     Start-Sleep -Seconds $RandomSeconds 

     InlineScript 
     { 
      $String = '{0:s}: Stopping number {1:D2} after {2:D2} seconds' -f (Get-Date),$Using:Number,$Using:RandomSeconds 
      Write-Host -Object $String 
     } 
    } 
} 
Test-Workflow 

您將得到類似以下的輸出:

2017-11-28T13:27:34: Starting number 09 for 25 seconds 
2017-11-28T13:27:34: Starting number 10 for 36 seconds 
2017-11-28T13:27:34: Starting number 08 for 53 seconds 
2017-11-28T13:27:35: Starting number 06 for 17 seconds 
2017-11-28T13:27:35: Starting number 07 for 28 seconds 
2017-11-28T13:27:35: Starting number 05 for 33 seconds 
2017-11-28T13:27:35: Starting number 04 for 49 seconds 
2017-11-28T13:27:35: Starting number 02 for 18 seconds 
2017-11-28T13:27:35: Starting number 03 for 47 seconds 
2017-11-28T13:27:35: Starting number 01 for 45 seconds 
2017-11-28T13:27:52: Stopping number 06 after 17 seconds 
2017-11-28T13:27:55: Starting number 11 for 49 seconds 
2017-11-28T13:27:55: Stopping number 02 after 18 seconds 
2017-11-28T13:27:55: Starting number 12 for 55 seconds 
2017-11-28T13:28:00: Stopping number 09 after 25 seconds 
2017-11-28T13:28:00: Starting number 13 for 37 seconds 
2017-11-28T13:28:03: Stopping number 07 after 28 seconds 
2017-11-28T13:28:03: Starting number 14 for 46 seconds 
2017-11-28T13:28:08: Stopping number 05 after 33 seconds 
2017-11-28T13:28:08: Starting number 15 for 48 seconds 
2017-11-28T13:28:11: Stopping number 10 after 36 seconds 
2017-11-28T13:28:11: Starting number 16 for 57 seconds 
2017-11-28T13:28:21: Stopping number 01 after 45 seconds 
2017-11-28T13:28:21: Starting number 17 for 22 seconds 
2017-11-28T13:28:22: Stopping number 03 after 47 seconds 
2017-11-28T13:28:22: Starting number 18 for 39 seconds 
2017-11-28T13:28:24: Stopping number 04 after 49 seconds 
2017-11-28T13:28:24: Starting number 19 for 34 seconds 
2017-11-28T13:28:28: Stopping number 08 after 53 seconds 
2017-11-28T13:28:28: Starting number 20 for 58 seconds 
2017-11-28T13:28:37: Stopping number 13 after 37 seconds 
2017-11-28T13:28:43: Stopping number 17 after 22 seconds 
2017-11-28T13:28:44: Stopping number 11 after 49 seconds 
2017-11-28T13:28:49: Stopping number 14 after 46 seconds 
2017-11-28T13:28:50: Stopping number 12 after 55 seconds 
2017-11-28T13:28:56: Stopping number 15 after 48 seconds 
2017-11-28T13:28:58: Stopping number 19 after 34 seconds 
2017-11-28T13:29:01: Stopping number 18 after 39 seconds 
2017-11-28T13:29:08: Stopping number 16 after 57 seconds 
2017-11-28T13:29:26: Stopping number 20 after 58 seconds 

正如你可以從輸出中看到的,循環的10個實例併發甚至跑步儘管我沒有運行10個PowerShell.exe實例。您可以使用InlineScript(如上例中所示)來直觀地查看工作流何時啓動和/或停止循環的實例。

+0

爲什麼使用2個inlinecript塊? – Atf

+0

@Atf我正在使用Inline腳本塊來顯示每次迭代的開始和停止時間。正如你從輸出中看到的那樣,它表明有10個併發迭代正在運行。 –

+0

感謝您的回答。但在你的情況下,內聯腳本中的命令不會創建任何進程。這是一個簡單的寫主機的東西。我一直使用PowerShell在我的內聯腳本中調用不同的批處理文件,行爲是一樣的。我的問題仍然只有5個線程的powershell可以在任務管理器中看到,這是真實的。編輯問題澄清 – Atf