gpt4 book ai didi

powershell - 在powershell中运行N个并行作业

转载 作者:行者123 更新时间:2023-12-03 00:00:44 27 4
gpt4 key购买 nike

我有以下 powershell 脚本

$list = invoke-sqlcmd 'exec getOneMillionRows' -Server...
$list | % {
GetData $_ > $_.txt
ZipTheFile $_.txt $_.txt.zip
...
}

如何在有限的最大作业数的情况下并行运行脚本 block ({ GetDatta $_ > $_.txt ....}),例如一次最多可以生成8个文件?

最佳答案

与用户“Start-Automating”发布的想法相同,但纠正了在他的示例中忘记启动在点击 else 子句时被阻止的作业的错误:

$servers = @('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n')

foreach ($server in $servers) {
$running = @(Get-Job | Where-Object { $_.State -eq 'Running' })
if ($running.Count -ge 4) {
$running | Wait-Job -Any | Out-Null
}

Write-Host "Starting job for $server"
Start-Job {
# do something with $using:server. Just sleeping for this example.
Start-Sleep 5
return "result from $using:server"
} | Out-Null
}

# Wait for all jobs to complete and results ready to be received
Wait-Job * | Out-Null

# Process the results
foreach($job in Get-Job)
{
$result = Receive-Job $job
Write-Host $result
}

Remove-Job -State Completed

关于powershell - 在powershell中运行N个并行作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8781666/

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