Home > PowerShell, VMWare > Connect to Multiple vCenter Servers with Password File

Connect to Multiple vCenter Servers with Password File

 

I’ve been getting tired lately of having to use the “get-credential” cmdlet in Powershell when I want to connect to various items. Thanks fully after some research I came across a cmdlet that is provided as part of the PowerCLI Toolkit. Thanks to Chris Nakagaki (http://tech.zsoldier.com/2011/09/save-powercli-login-credentials-to-xml.html) for originally posting about this a year ago. I am going to take his instructions a step further and show you how to not only create the file but also connecto to multiple vCenter/ESX Servers at one time.

First we need to create and add content to our new xml file


# Create VI Credential File
New-VICredentialStoreItem -Host vCenterServerFQDN -File C:\PATHTOFILE\xmlfile.xml -User username -Password password

* As you may notice the string above will require that your password be viewed in plaintext on the screen, I would suggest not saving this but simply just creating this from the PowerCLI Shell and then once you’re done adding your entries closing that shell.

You want to run the above string as many times as you you need to. For my environment I only have 2 production vCenter servers that I need to connect to, so I ran the command twice providing the information needed for both vCenter servers.

Now let’s connect to the vCenter Servers.

You can do this one of 2 ways:

1. Connect to all vCenter Servers in the XML File


Get-VICredentialStoreItem -File c:\PATHTOFILE\xmlfile.xml | %{
Connect-VIServer -Server $_.host -User $_.User -Password $_.Password
}

2. Connect to an array of vCenter Servers which are defined in a Powershell Array


# Using the VI Credential File for multiple vCenter Servers
# vCenterServerNames have to match what is listed as the hostname in the VI Credential File

$Hosts = @("vCenterServerA","vCenterServerB")  $Hosts | %{
$creds = Get-VICredentialStoreItem -File c:\PATHTOFILE\xmlfile.xml -Host $_
Connect-VIServer -Server $creds.host -User $creds.User -Password $creds.Password
}
$creds = $null

Advertisements
  1. Tom
    December 12, 2014 at 7:15 pm

    So this is driving me crazy….I am trying to script a utility file that will prompt me to choose which server (or servers) I want to connect to and then use the credentials to log into it/them. It is very similar to what you have done already, but when I did it, it only accepts the first credential in the file. Subsequent credential are searched then ignored and instead continue to prompt for credentials. I have tested every other part of the script and it is specifically with the VICredentialStoreItem and is dependent on what order the machines are stored as to which, if any, chosen server will log in automatically. The rest will prompt me. I even tested to see if it would log in the first server chosen if it wasn’t the first in the credential store and it does not. Any ideas on why this behavior exists?

    • December 12, 2014 at 11:19 pm

      Email me what you got and ill take a look at it. Also, send me your xml file (please change username and passwords listed in it) email: josh@mx.gozettabyte.com.

      • Tom
        December 15, 2014 at 2:51 pm

        Thank you. Sending shortly.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: