Powershell 并行调用命令不起作用

Dee*_*rma 1 powershell

我正在尝试文档中指定的一个非常简单的 PowerShell 并行示例。

1..5 | ForEach-Object -Parallel { "Hello $_"; sleep 1; } -ThrottleLimit 5 
Run Code Online (Sandbox Code Playgroud)

但它给出了以下错误,这方面的任何帮助都将被适当。

ForEach-Object : Parameter set cannot be resolved using the specified named parameters.
At C:\temp\parallel.ps1:2 char:9
+ 1..10 | ForEach-Object -Parallel {
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : MetadataError: (:) [ForEach-Object], ParameterBindingException
    + FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.ForEachObjectCommand
Run Code Online (Sandbox Code Playgroud)

以下是我的 PowerShell 版本信息

Name                           Value
----                           -----
PSVersion                      5.1.18362.752
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.18362.752
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
Run Code Online (Sandbox Code Playgroud)

sta*_*tor 5

ForEach-Object -Parallel已在 PowerShell 7.0 中引入。您正在使用 PowerShell 5.1。这就是为什么它不起作用。

如果您并行安装了 PowerShell 7.0(这是可能的),请直接调用 PS 可执行文件并使用它执行您的脚本。