PowerShell Script to Login to Multiple vCenter Servers

Do you have multiple vCenter servers that you interact with on a daily basis? Do you like to use PowerShell to manage them? I was opening a PowerShell window, and doing a connect-viserver for each and having to enter my PW over and over again.

First, I tried to use the New-VICredentialStoreItem command to store my PW, but with security breaches being rampant in the field, I decided I didn’t want my stored creds to get me in trouble. If you’d like to learn how to use this command, this is how I do it…

# Get Credential and store it in a variable 
$creds = Get-Credential 
# Use above credential to not have to pass your PW in open text. 
New-VICredentialStoreItem -Host (your vCenter/ESXi name/ip)       
     -User (your username & with domain if necessary)       
     -Password $creds.Password 

I took that to the next level with the script below. I capture the credential in a variable (similar to above) and then use that variable to connect to my vCenters. I pre-fill the username so I don’t have to type it each time. The -message field is required when using the -username field.

# capture credentials
$creds = get-credential -username USERNAME@DOMAIN.COM 
     -Message "Enter your admin creds"

# Login to each vCenter
Connect-VIServer vCenter1, vCenter2, vCenter3, vCenter4 
     -Credential $creds

I hope someone finds this helpful

Ben Liebowitz, VCP, vExpert
NJ VMUG Leader

Share This:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.