6

我正在尝试触发一个 powershell 工作流,它应该并行旋转 10 个线程。我正在使用 PS 版本 4。代码 -

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

我在我的内联脚本中使用 powershell 调用批处理文件。我在任务管理器中观察到的是,一次只有 5 个线程处于活动状态-

在此处输入图像描述

可能是下一个线程被拾起,只有五个完成了。我从未见过超过 5 ps 的实例。但是,当我检查每个用户允许的 ps 会话时,它远远超过 5。

在此处输入图像描述

如何在 PS 工作流程中并行旋转 10 个 PS 线程。我在这里想念什么?

4

3 回答 3

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 个 powershell.exe 实例,也有 10 个循环实例同时运行。您可以使用InlineScript上面我的示例中的方法来直观地查看您的工作流程何时开始和/或停止循环实例。

于 2017-11-28T19:32:46.720 回答
1

如果我正确理解您的问题,是的,在工作流中启动 powershell 进程和运行内置 powershell(模拟)命令之间似乎存在差异。它似乎将任务管理器中的进程计数限制为 5。也许您可以使用正在运行的内容更新您的示例。也许您更喜欢工作或启动过程。

 workflow work { foreach -parallel ($i in 1..10) { powershell "sleep 10; echo done $i" }  }
 measure-command { work } | fl seconds

 Seconds           : 35

 workflow work { foreach -parallel ($i in 1..10) { sleep 10; echo done $i }  }
 measure-command { work } | fl seconds

 Seconds           : 14

编辑:

嗯,可以的。它有点涉及。您必须是管理员,并且必须启用 psremoting。虽然我看到更多的进程在运行,但实际上完成的时间更长。

# all default to 5
$wfopt = New-PSWorkflowExecutionOption -MaxSessionsPerWorkflow 20 -MaxSessionsPerRemoteNode 20 -MaxActivityProcesses 20

Register-PSSessionConfiguration -Name myworkflow -SessionTypeOption $wfopt -SessionType Workflow -Force

work -PSConfigurationName myworkflow

您可以使用 Set-PSSessionConfiguration 对其进行修改,使用 Get-PSSessionConfiguration 对其进行检查,并使用 UnRegister-PSSessionConfiguration 将其删除。

您也可以修改默认配置 Microsoft.PowerShell.Workflow,但更改是永久性的。

于 2019-08-02T17:59:49.623 回答
0

我使用 PS v5.1 仍然有同样的限制。有没有办法绕过一些较新版本的 PS 的这些限制?也许在PS6上?

Name                           Value
----                           -----
PSVersion                      5.1.17134.858
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.17134.858
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

按照代码和结果:

foreach -parallel -throttlelimit 10 ($i in 0..20)
{
    "start #$i"
    InlineScript { "    inline #$using:i" }
}

start #9
start #8
start #7
start #6
start #5
start #4
start #3
start #2
start #1
start #0
    inline #
    inline #
    inline #
    inline #
    inline #
start #10
start #11
start #12
start #13
start #14
    inline #
    inline #
    inline #
    inline #
    inline #
start #15
start #16
start #17
start #18
start #19
    inline #
    inline #
    inline #
start #20
    inline #
    inline #
    inline #
    inline #
    inline #
    inline #
    inline #
    inline #
于 2019-08-02T14:02:38.813 回答