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:PATHTOFILExmlfile.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:PATHTOFILExmlfile.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:PATHTOFILExmlfile.xml -Host $_ Connect-VIServer -Server $creds.host -User $creds.User -Password $creds.Password } $creds = $null
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?
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.
Thank you. Sending shortly.