6

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.

Acerbity
  • 417
  • 1
  • 11
  • 29
  • 2
    `-ArgumentList $group.Group.ServerName, $cred` should suffice. What PowerShell version are you using? – Ansgar Wiechers Aug 21 '15 at 17:47
  • Powershell 4. If I try to pass it like that, the job hangs forever (or at least longer than 20 minutes). – Acerbity Aug 21 '15 at 18:07
  • One Additional note, if I just try to run the command with a regular array outside of the loop, the job still hangs. Start-Job -ScriptBlock $bounceBlock -ArgumentList $stuff,$cred – Acerbity Aug 21 '15 at 18:14
  • Did @($group.Group.ServerName) not work for you? The unary operator probably would have a one element array that contained an array. – Matt Aug 31 '15 at 17:27
  • Well, I created an array of servers and tried passing it manually outside of the script (just sent it to the 'bounceblock' directly) and that didn't work either. I think the issue is in the action itself. – Acerbity Aug 31 '15 at 18:05
  • What's the actual error you get? – briantist Sep 01 '15 at 21:11
  • Beyond the answer I gave; I will also share find that naming the jobs is a good practice as well so you can locate them, stop them and kill them later. In your scenario, I use a naming convention of servername+action. It helps if others are administering jobs and come across yours. Not that they should be deleting jobs they have no business with but a descriptive name will make them think twice if they easily can tell what the job is related to. – SDillon Sep 02 '15 at 12:40
  • @briantist I am not sure. The jobs start and never complete. Perhaps this is also an issue where I am not knowledgeable enough about determining what the error actually is. – Acerbity Sep 02 '15 at 17:04
  • Have you tried running the bounceBlock as a loop rather than as a job? Just to see if it works? – antonyoni Sep 04 '15 at 08:00
  • I have. It works just fine standalone. – Acerbity Sep 04 '15 at 13:37
  • From what I see we can't tell what `$targets` is. My question is what is the type of `$group.Group.ServerName` in the loop? `$group.Group.ServerName.GetType().Fullname`. Also seems redundant to run `Connect-VIServer` in the block every time. Wonder if multiple sessions are not allowed. – Matt Sep 04 '15 at 15:34
  • I thought of that too... I tried running it with only enough servers for one session, and that still didn't work. – Acerbity Sep 08 '15 at 15:28

2 Answers2

1

I am certainly no expert with PS, but first to address how you're passing in the appended list of servers; I do something similar with Azure VMs using Get-AzureVM and I pass in my list of VM names in a System.Array to functions or cmdlets such as a variable such as $theVMs= "MyServer1","MyServer2","MyServer3" and then I execute a foreach loop over ($vm in $theVMs) and then perform the actions such as Get-VM sequentially. I do this sequentially since PS has some much lower limits, 5 per my experience, doing this via a parallel for loop.

A typical way I interact with the VMs remotely and create a PS Job per each is to use $uri = Get-AzureWinRMUri -ServiceName $svc -Name $vmname

 Invoke-Command -ConnectionUri $uri -Credential $creds **-JobName 
 $jobname**    
 -ArgumentList $vmname -ScriptBlock {
 param([string]$thevm) ...
 }

This requires the InstallWinRMCertAzureVM.ps1 script which is discussed and available at http://blogs.technet.com . I use this for between 30 servers regularly.

SDillon
  • 213
  • 1
  • 11
0

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.

Acerbity
  • 417
  • 1
  • 11
  • 29