Search code examples
arrayspowershellpowercli

Passing an array to Get-Job


I am having trouble getting an array passed to a scriptblock in Start-Job. Can you tell me what I might be doing wrong?

$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
    }
}

I basically split an array into chunks of 50 (working) then try to run them as jobs. The error I get looks like it's trying to run Get-VM for a single server, named all 50 values appended together.


Solution

  • Just wanted everyone to know in case they run into a similar problem, this is an issue with running Get-VM, and it exists whether running it in a job or workflow. VMWare is aware of this issue.