我無法在Start-Job中將數組傳遞給scriptblock。你能告訴我我可能做錯了什麼嗎?將數組傳遞給Get-Job
$bounceBlock = {
param(
[string[]]$list,
[System.Management.Automation.PSCredential]$cred
)
Add-PSSnapin VMware.VimAutomation.Core | Out-Null
Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Scope User -InvalidCertificateAction Ignore -Confirm:$false | Out-Null
Connect-VIServer -Server servername -Credential $cred -AllLinked
Get-VM -Name $list
}
if ($targets) {
$activeTargets = $targets | Get-Random -Count $prodTargets.Count
$counter = [pscustomobject] @{Value = 0}
$groupSize = 50
$groups = $activeTargets | Group-Object -Property {[math]::Floor($counter.Value++/$groupSize)}
$connection = Connect-VIServer -Server servername -Credential $cred -AllLinked
if ($connection -match "servername") {
foreach ($group in $groups) {
while ((Get-Job -State Running).Count -ge 5) {
Start-Sleep -Seconds 5
}
Start-Job -ScriptBlock $bounceBlock -ArgumentList (,$group.Group.ServerName),$cred
}
Disconnect-VIServer * -Force -Confirm:$false
}
}
我基本上把一個數組分成50塊(工作),然後嘗試運行它們作爲工作。我得到的錯誤看起來像是試圖爲單個服務器運行Get-VM,將所有50個值命名爲一起。
'-ArgumentList $ group.Group.ServerName,$ cred'應該就足夠了。您使用的是什麼PowerShell版本? –
Powershell 4.如果我試圖通過它,這項工作永遠掛起(或至少超過20分鐘)。 – Acerbity
一個額外的說明,如果我只是嘗試運行循環外的常規數組的命令,作業仍然掛起。 Start-Job -ScriptBlock $ bounceBlock -ArgumentList $ stuff,$ cred – Acerbity