VMware Cloud Community
SNEdwards
Contributor
Contributor
Jump to solution

Multiple VCenter servers, "ForEach" loop?

I need help with the syntax to modify some of my existing scripts to connect to multiple VCenters servers that we now have in my environment. Something along the lines of:

$VCenters = VIServer1, VIServer2, etc.

Foreach($VIserver in $VCenters)

{

Connect-VIServer $VIserver

EXISTING SCRIPT HERE

}

Any input on how I can accomplish this?

Thanks

Reply
0 Kudos
1 Solution

Accepted Solutions
FranckRookie
Leadership
Leadership
Jump to solution

Hi SNEdwards,

I do it this way:

$vCenters = VIServer1, VIServer2, etc.

ForEach ($vCenter in $vCenters) {

connect-VIServer $vCenter -User $VcUser -Password $VcUserPassd

... code to run against each vCenter...

disconnect-VIserver -confirm:$false
}

Hope it helps.

Regards

Franck

View solution in original post

Reply
0 Kudos
5 Replies
FranckRookie
Leadership
Leadership
Jump to solution

Hi SNEdwards,

I do it this way:

$vCenters = VIServer1, VIServer2, etc.

ForEach ($vCenter in $vCenters) {

connect-VIServer $vCenter -User $VcUser -Password $VcUserPassd

... code to run against each vCenter...

disconnect-VIserver -confirm:$false
}

Hope it helps.

Regards

Franck

Reply
0 Kudos
avlieshout
VMware Employee
VMware Employee
Jump to solution

You can connect to multiple vcenters at once. Just specify their names separated by a colon.

Connect-VIServer VIServer1,VIServer2

The server connection mode needs to be set to multiple for this to work.

Set-PowerCLIConfiguration -DefaultServerMode "multiple"

~Arnim

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
Reply
0 Kudos
SNEdwards
Contributor
Contributor
Jump to solution

Hi SNEdwards,

I do it this way:

> $vCenters = VIServer1, VIServer2, etc.
> 
> ForEach ($vCenter in $vCenters) {
> 
> connect-VIServer $vCenter -User $VcUser -Password $VcUserPassd
> 
> ... code to run against each vCenter...
> 
> disconnect-VIserver -confirm:$false
> }
> 
> 

Hope it helps.

Regards

Franck

That's what I was trying to do, but got the following error:

The term 'VISERVER1' is not recognized as a cmdlet, function, operable program, or script. Verify the term and try again.

At C:\scripts\test\cluster_summary.ps1:6 char:32

+ $vCenters = VISERVER1, <<<< VISERVER2

Connect-VIServer : The argument cannot be null or empty.

Reply
0 Kudos
admin
Immortal
Immortal
Jump to solution

Hi,

just put the names of VC servers in quotes, like this:

$vCenters = 'VIServer1', 'VIServer2'

Vitali

PowerCLI Team

FranckRookie
Leadership
Leadership
Jump to solution

I reused your example but in fact I initiate my variable this way:

# vCenter list
$VCenters = @()
$VCenters += "VIServer1"
$VCenters += "VIServer2"
$VCenters += "VIServer3"
...

Vitali's code is shorter, but as I regularly need to avoid one or two servers, I just need to comment the corresponding line.

Just a comment about Arnim's post: connecting to several vCenters this way works perfectly. But in your code, you need to take into account that you could have several objects with the same name, what is not possible connected to a single vCenter.

Sorry if my first post was not complete.

Regards

Franck

Reply
0 Kudos