Some PowerCLI 101 – Connecting to Multiple vCenters

A few things brought this post together. First is that Glen Sizemore of Get-Admin and I have co-submitted a “vSphere Automation 101 – PowerCLI” session to the VMworld 2011 CFP. We thought it’d be an excellent idea to share some of that content before hand.

The second, and just as important, is that I’m Lazy so in the same line as my other “Professionally Lazy” post, here is another 101 style post for PowerCLI. Basically I dislike typing out the names of every vCenter each time I need to connect to more than one. PowerCLI is flexible enough to allow you to connect to more than one at a time, so why not take advantage. As with everything in IT, there are multiple ways to eat the cat… or skin him or whatever:

Lots of vCenters in Connect String

This is the most straight forward of the bunch:

Connect-VIServer -Server vcenter01.provmware.com, vcenter02.provmware.com

Storing vCenters in an Array

Well, using a list like above, that’s a lot of typing, and other than blog posts, why type more than you have to? Another approach is to keep all of your vCenters in an array, and then connect to them all at once. This is a variation on the above in that it keeps your connect line short:

# Setup array with hosts
$hosts = @(
    "vCenter01.ProVMware.com",
    "vCenter02.ProVMware.com",
    "vCenter03.ProVMware.com"
);

# Store your U&P
$user = "notTelling"
$password = "butNiceTry"

# Connect
Connect-VIServer -Server $hosts -User $user -Password $password

Using a Menu

This was one of those “learn something new every day” features. As in, I the blog post surfaced about 6 minutes after I started typing this post up, and that is, the -Menu parameter of connect-viserver. Using their example:

PS C:\WINDOWS\system32\windowspowershell\v1.0> Connect-VIServer -Menu
Select a server from the list (by typing its number and pressing Enter):
[1] aevrov-mvc-3.vmware.com
[2] localhost
[3] S10
[4] vvvvc41-ga.pc
[5] 10.23.37.6
[6] vc41-ga.pc
[7] 10.23.113.103
[8] 10.23.33.0
[9] aevrov-mvc-2.vmware.com
[10] S9
6
WARNING: There were one or more problems with the server certificate:
* The X509 chain could not be built up to the root certificate.
* The certificate’s CN name does not match the passed value.

Name                           Port  User
—-                           —-  —-
vc41-ga.pc                     443   powershell

Summary

With luck, this will get you a bit more familiar with PowerCLI and managing your environment, of any size. Stay tuned for more.

4 thoughts on “Some PowerCLI 101 – Connecting to Multiple vCenters

  • how do i run commands specific to one VCenter when i’m connected to many VCenters?

  • Some cmdlets have a -VIServer parameter. For example, this parameter can use the iterations in your $global:DefaultVIServer variable as input so you can target different vCenter instances in this way.

    Get-VM -VIServer $global:defaultVIServer[0]
    Get-VM -VIServer $global:defaultVIServer[1]

    If there are cmdlets that don’t have the -VIServer parameter, they most likely will receive input from earlier cmdlets that do.

    Get-VM -VIServer $global:defaultVIServer[0] | Get-HardDisk

    Other cmdlets use -Server instead of -VIServer such as Get-View. Notice when you do a Get-Help Get-View that there is a [VIServer[]] object listed for this parameter.

Comments are closed.