在 powershell 中运行 N 个并行作业 [英] Run N parallel jobs in powershell
本文介绍了在 powershell 中运行 N 个并行作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下 powershell 脚本
I have the following powershell script
$list = invoke-sqlcmd 'exec getOneMillionRows' -Server...
$list | % {
GetData $_ > $_.txt
ZipTheFile $_.txt $_.txt.zip
...
}
如何在有限的最大作业数下并行运行脚本块 ({ GetDatta $_ > $_.txt ....}
),例如一次最多可以生成8个文件?
How to run the script block ({ GetDatta $_ > $_.txt ....}
) in parallel with limited maximum number of job, e.g. at most 8 files can be generated at one time?
推荐答案
Start-Job cmdlet 允许您在后台运行代码.要按照您的要求执行操作,应该可以使用以下代码.
The Start-Job cmdlet allows you to run code in the background. To do what you'd ask, something like the code below should work.
foreach ($server in $servers) {
$running = @(Get-Job | Where-Object { $_.State -eq 'Running' })
if ($running.Count -le 8) {
Start-Job {
Add-PSSnapin SQL
$list = invoke-sqlcmd 'exec getOneMillionRows' -Server...
...
}
} else {
$running | Wait-Job
}
Get-Job | Receive-Job
}
希望这会有所帮助.
这篇关于在 powershell 中运行 N 个并行作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文