LucD
Leadership
Leadership

I'm afraid not, the Connect-VIServer cmdlet doesn't have a RunAsync switch.

In other words, you can't run that cmdlet in the background, and hence in parallel.

You could use the Start-Job cmdlet to run the Connect-VIServer cmdlets in the background.

And then use the SessionId to connect in the caller context to these sessions.

But I have my serious doubts that this would be faster in an environment with only a couple of connects and then for 3 seconds.

The Start-Job has some overhead as well, and the connect with the SessionId will also take time.

$vcNames = 'vc1','vc2','vc3'

if($global:DefaultVIServers){

    Disconnect-VIServer -Server $global:DefaultVIServers.Name -Confirm:$false

}

$code = {

param($vc)

Connect-VIServer -Server $vc | %{"$($_.Name),$($_.SessionId)"}

}

$jobs = foreach($vc in $vcNames){

    Start-Job -Name vcConnect -ScriptBlock $code -ArgumentList $vc | Select -ExpandProperty Id

}

Receive-Job -Wait -Id $jobs | %{

    Connect-VIServer -Server $_.Split(',')[0] -Session $_.Split(',')[1]

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post