Sto avendo difficoltà a raggiungere una matrice passata a una scriptblock in Start-Job. Puoi dirmi cosa potrei fare di sbagliato?Passaggio di un array a 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
}
}
Io fondamentalmente diviso un array in blocchi di 50 (di lavoro) quindi provare a eseguirli come lavori. L'errore che appare sembra provochi l'esecuzione di Get-VM per un singolo server, con il nome di tutti e 50 i valori aggiunti insieme.
'-ArgumentList $ group.Group.ServerName, $ cred' dovrebbe essere sufficiente. Quale versione di PowerShell stai usando? –
PowerShell 4. Se provo a passarlo in questo modo, il lavoro si blocca per sempre (o almeno per più di 20 minuti). – Acerbity
Una nota aggiuntiva, se cerco semplicemente di eseguire il comando con un array normale al di fuori del ciclo, il processo si blocca ancora. Start-Job -ScriptBlock $ bounceBlock -ArgumentList $ stuff, $ cred – Acerbity