VMware Cloud Community
vandreytrindade
Enthusiast
Enthusiast
Jump to solution

Is there a way to connect to multiple VMware servers in parallel using PowerCLI?

Hi,

I was just wondering if I can connect simultaneously on multiple VMware servers.

Today I'm using a foreach with the Connect-VIServer cmdlet, but it has to go though the entire list of servers.

Att, Vandrey Trindade
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

9 Replies
LucD
Leadership
Leadership
Jump to solution

The Server parameter on the Connect-VIServer cmdlet accepts multiple values (an array).

So you can do

Connect-VIServer -Server vc1,vc2,vc3


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

vandreytrindade
Enthusiast
Enthusiast
Jump to solution

LucD​,

Hi, thanks for replying.

I did the test using a foreach loop and using what you said.

Both tests behave the same.

But that's ok. I thought there was a way to connect to all the servers at once.

Thanks!

Att, Vandrey Trindade
0 Kudos
LucD
Leadership
Leadership
Jump to solution

But there is, in my previous example you call Connect-VIServer once, and you connect to all 3 vSphere servers.

To avoid being prompted for credentials, you can store the credentials for all 3 vSphere servers in a VICredentialStoreItem.

# Do this only once to create the credential store items

New-VICredentialStoreItem -Host vc1 -User uservc1 -Password Password1!

New-VICredentialStoreItem -Host vc2 -User uservc2 -Password Password2!

New-VICredentialStoreItem -Host vc3 -User uservc3 -Password Password3!

# Connect to 3 vCenters in 1 call

Connect-VIServer -Server vc1,vc2,vc3

Or did you see something different in your tests?


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

kwhornlcs
Enthusiast
Enthusiast
Jump to solution

If the vCenters are in enhanced linked mode you could also try

Connect-Viserver vc1 -AllLinked

vandreytrindade
Enthusiast
Enthusiast
Jump to solution

LucD​,

Sorry... I didn't receive e-mail notifications of your reply.

What I thought I could get was:

If I try to connect to one server using: Connect-VIServer -Server vc1

It takes 3 seconds or less to connect to it.

If I try to connect to two servers or more using: Connect-VIServer -Server vc1,vc2

It takes 6 seconds or less to connect to all of them.

I wanted to know if there was a way of connecting to all servers using only the "3 seconds or less" time frame.

Just curiosity...

Att, Vandrey Trindade
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

vandreytrindade
Enthusiast
Enthusiast
Jump to solution

LucD​,

I really appreciate your time and attention.

Thanks!

Att, Vandrey Trindade
0 Kudos
mtnbkr0918
Enthusiast
Enthusiast
Jump to solution

I can connect to multiple vCenters without any issues. However, how would I run a connect that only is applied to 1 or 2 vCenters. 

Say I have VC1, VC2, VC3, VC4, and VC5. I want to add a Custom attribute only to VC3 and VC5. How would I target those vCenters

 

 

New-CustomAttribute  -Name "MyCustomAttribute" -TargetType VMHost

 

as always thanks in advance

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Use the Server parameter

New-CustomAttribute  -Name "MyCustomAttribute" -TargetType VMHost -Server VC3,VC5


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

0 Kudos