我正在尝试触发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线程。 我在这里缺少什么?
答案 0 :(得分: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
来直观地了解工作流程何时启动和/或停止循环实例。
答案 1 :(得分: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 #
答案 2 :(得分:0)
如果我正确理解了您的问题,是的,启动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,但是更改是永久的。