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
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
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
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
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.
Hi,
just put the names of VC servers in quotes, like this:
$vCenters = 'VIServer1', 'VIServer2'
Vitali
PowerCLI Team
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