如何并行执行 PowerShell 函数多次? [英] How to execute a PowerShell function several times in parallel?

查看:22
本文介绍了如何并行执行 PowerShell 函数多次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定是否将其称为多线程、基于作业或异步的需求,但基本上我有一个 Powershell 脚本函数,它接受多个参数,我需要使用不同的参数多次调用它,并且让这些并行运行.

I'm not sure whether to call this a need for multi-threading, job-based, or async, but basically I have a Powershell script function that takes several parameters and I need to call it several times with different parameters and have these run in parallel.

目前,我这样调用函数:

Currently, I call the function like this:

Execute "param1" "param2" "param3" "param4"

如何在不等待每次调用 Execute 的情况下多次调用它?

How can I call this multiple times without waiting for each call to Execute return to the caller?

目前我运行的是 v2.0,但如有必要我可以更新

Currently I'm running v2.0 but I can update if necessary

这是我到目前为止所拥有的,但不起作用:

here's what I have so far, which doesn't work:

$cmd = {
    param($vmxFilePath,$machineName,$username,$password,$scriptTpath,$scriptFile,$uacDismissScript,$snapshotName)
    Execute $vmxFilePath $machineName $username $password $scriptTpath $scriptFile $uacDismissScript $snapshotName
}

Start-Job -ScriptBlock $cmd -ArgumentList $vmxFilePath, $machineName, $username $password, $scriptTpath, $scriptFile, $uacDismissScript, $snapshotName

我收到一个错误:

无法将system.object[]"转换为参数initializationscript"所需的类型system.management.automation.scriptblock".不支持指定的方法

cannot convert 'system.object[]' to the type 'system.management.automation.scriptblock' required by parameter 'initializationscript'. specified method is not supported

我已经修改了我的脚本,但仍然出现上述错误.这是我的模组:

I've modified my script but I still get the error mentioned above. Here's my mod:

$cmd = {
    param($vmxFilePath,$machineName,$username,$password,$scriptTpath,$scriptFile,$uacDismissScript,$snapshotName)
    Execute $vmxFilePath $machineName $username $password $scriptTpath $scriptFile $uacDismissScript $snapshotName
}

Start-Job -ScriptBlock $cmd -ArgumentList $vmxFilePath, $machineName, $username $password, $scriptTpath, $scriptFile, $uacDismissScript, $snapshotName

推荐答案

不需要更新.定义一个脚本块并使用 Start-Job 根据需要多次运行脚本块.示例:

No update necessary for this. Define a script block and use Start-Job to run the script block as many times as necessary. Example:

$cmd = {
  param($a, $b)
  Write-Host $a $b
}

$foo = "foo"

1..5 | ForEach-Object {
  Start-Job -ScriptBlock $cmd -ArgumentList $_, $foo
}

脚本块采用 2 个参数 $a$b,它们由 -ArgumentList 选项传递.在上面的例子中,赋值是 $_$a$foo$b.$foo 只是一个可配置的静态参数示例.

The script block takes 2 parameters $a and $b which are passed by the -ArgumentList option. In the example above, the assignments are $_$a and $foo$b. $foo is just an example for a configurable, but static parameter.

运行 Get-Job |Remove-Job 在某个时候从队列中删除完成的作业(或 Get-Job | % { Receive-Job $_.Id; Remove-Job $_.Id } 如果您想检索输出).

Run Get-Job | Remove-Job at some point to remove the finished jobs from the queue (or Get-Job | % { Receive-Job $_.Id; Remove-Job $_.Id } if you want to retrieve the output).

这篇关于如何并行执行 PowerShell 函数多次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆