VMware Cloud Community
mike-p
Enthusiast
Enthusiast
Jump to solution

Change vCenter

How is the usage of commands if i am connected to multiple vCenters ?

Often i want to get the result of a get-command only for one vCenter although i am connected to multiple vCenters.

Do i have to disconnect / connect first in this case ?

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

There is no need to keep all your connections in separate variables, they are already stored in $defaultVIServers.

With a simple where clause you can retrieve the connection you want.

For example:

Get-VM -Server ($defaultVIServers | where {$_.Name -eq "vCenter1"})


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

View solution in original post

0 Kudos
10 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

If you are connected to multiple vCenter servers, but want to the Get command only return the results of one vcenter server, you can use the -Server parameter. Like in:

$VIServer = Connect-VIserver "MyvCenterServer"
Get-VM -Server $VIserver

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
LucD
Leadership
Leadership
Jump to solution

When you use the Multi option for DefaultVIServerMode you can use the -Server parameter on most cmdlets to specify on which connection you want to execute the cmdlet. No need to dicsonnect-connect all the time.

For example

Get-VM -Server $defaultVIServers[1]

will use the 2nd connection from the list.

You can check the multi setting with Get-PowerCLIConfiguration and change it with Set-PowerCLIConfiguration.


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

Paule83
Enthusiast
Enthusiast
Jump to solution

When you are connected to multiply VCenter servers, you can use the "-server VCENTER" paramter to specify the VCenter you need the information from.

No need for any connect or disconnect commands.

EDIT: too slow, crappy proxy in the office...

Nachricht geändert durch Paule83

If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful
0 Kudos
mike-p
Enthusiast
Enthusiast
Jump to solution

This is a good option - now i only need a command switch to display numbers before the servernames of $global:DefaultVIServers  🙂

0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Maybe my example was not clear enough. Try it like this to use servernames:

$Production = Connect-VIserver "Production"
$OTA        = Connect-VIserver "OTA"
Get-VM -Server $Production
Get-VM -Server $OTA

Message was edited by: RvdNieuwendijk

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
LucD
Leadership
Leadership
Jump to solution

There is no need to keep all your connections in separate variables, they are already stored in $defaultVIServers.

With a simple where clause you can retrieve the connection you want.

For example:

Get-VM -Server ($defaultVIServers | where {$_.Name -eq "vCenter1"})


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

0 Kudos
mike-p
Enthusiast
Enthusiast
Jump to solution

yes,

i often work in foreign VMware-systems.  In your approach i have to define the servernames first.

In Luc's i can use the listed servers immediatly. FYI : sometimes up to 10 vCenters.

0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

I must say that I like Luc's solution also. Only if you use this construction more than once in one script, it will be faster to get the vCenter server object once at the beginning of the script instead of getting it every time again:

$vCenter1 = $defaultVIServers | Where-Object {$_.Name -eq "vCenter1"}
Get-VM -Server $vCenter1
Get-VMHost -Server $vCenter1

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Robert, both methods have their pros and cons.

Your method is indeed more optimal when you open all the vCenters at the beginning of the script and close them all again at the end of the script.

The method I showed is better if the script opens and closes vCenter or ESX(i) connections throughout the script.


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

0 Kudos
mike-p
Enthusiast
Enthusiast
Jump to solution

Nevertheless , you made a great job. All of your approaches were very helpful .

Thanks a lot to all for your fast responses.

0 Kudos