gpt4 book ai didi

powershell - 如何增加 VM 上的并发 PowerShell 实例数?

转载 作者:行者123 更新时间:2023-12-04 15:40:20 25 4
gpt4 key购买 nike

我正在尝试触发一个 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 个线程处于事件状态-

enter image description here

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

enter image description here

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

最佳答案

给出以下示例脚本:

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就像我上面的例子一样,可以直观地看到您的工作流程何时开始和/或停止循环实例。

关于powershell - 如何增加 VM 上的并发 PowerShell 实例数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43322687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com