<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

Connect Multiple vCenters together with PowerCLI with secure password as String

Published on
4,186 Points
1,186 Views
Last Modified:
This article is divided into two sections.

1) First describes how you can connect to server / service / device with Username and secure password in powershell.
2) Second part is to use secure credentials and connect to vCenters which are in Linked Mode or isolated environment.

Introduction


** Below article is very useful when you have large Vmware Infrastructure and you want to perform any search operation in VM Infrastructure, VM related operations, ESX host, datastore related operations etc.


-- You don't need to search on individual vCenters for your VMs or hosts once you connected to Multiple vCenter servers at the same time.


** The second part is applicable if all your isolated vCenters have the same username and password otherwise it will fail to connect.

           

There are two parts to this article


-- The first part will explain to you when you use given commands below you don't need to worry about entering a password in plain text. Command written will encrypt the password and it will not be in readable form.


-- The second part will show you how to connect multiple vCenter servers which are isolated and Linked.


** This script (Second Part) has been tested in Vmware PowerCLI. We can also use in Windows Powershell but you need to download and install Vmware Module (Online or Offline). 


Storing Credentials with secure password.


1) When you connect to any server using PowerShell, let say using Connect-VIServer command it prompts you for a User name and Password.


                                         


2) Now if you have multiple servers to connect to a single script, you may find it irritating to enter same Username and Password every time. Instead, you may want to save the password for future use.


3) Now let say you are using a PowerShell "Read-Host" command to store username and password in a variable but the password is not in hidden text or in a secure string as displayed below.



4) You can use below command line to enter a username and secure password and can save to a variable for future use.


$uname = Read-Host "Enter UserName" 
Read-Host "Enter Password" -AsSecureString | ConvertFrom-SecureString | Out-File cred.txt 
$password = Get-Content cred.txt | ConvertTo-SecureString 
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $uname,$password


Now see the below




Make sure the script you save and running have the administrator rights to create the cred.txt file.


Now you can use $cred value to connect to server(s).


For Example : 

Connect-VIServer -Server myvcenter -Protocol https -Credential $cred




The above example gives an idea to connect any server with a username and password as a secure string.


Let assume your environment has multiple vCenter servers and they may be isolated or they may be in linked mode (All vCenters connected to linked mode vCenter). 


Follow teh steps below to do the same.


Connecting Multiple vCenter servers using VmWare PowerCLI


1) Assuming you went through the first part and used below script to store credentials.


Write-Host "Enter vCenter Credentials" 
$uname = Read-Host "Enter UserName" 
Read-Host "Enter Password" -AsSecureString | ConvertFrom-SecureString | Out-File cred.txt 
$password = Get-Content cred.txt | ConvertTo-SecureString 
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $uname,$password


2) If your VMware infrastructure is in Linked Mode (Multiple vCenters connected together). Use the below single liner script to connect Linkedmode vCente server.


Connect-VIServer -Server MyLinkedvCenterName -Protocol https -AllLinked -Credential $cred


3) If your vCenters are not in LinkedMode then use the script below to connect them together.


Same $cred will be applied to all vCenter if they have same username and password.


Script:


$vcenters = "vc1", "vc2", "vc3" #list of vCenter servers 
foreach ($vc in $vcenters) 
{     
    if( Connect-VIServer -server $vc -Protocol https -Credential $cred -ErrorAction Ignore)
    {        
        Write-Host "vCenter $vc Connected"  -ForegroundColor Cyan     
 }
    else
  {
         Write-Host "Failed to Connect vCenter $vc"  -ForegroundColor Cyan
    }    
}



-- Once you are connected to vCenters you can easily search or perform any operations using the command line of any of the VMs / ESXi / Datastores etc which are in any of the vCenters.


I encourage you to share your valuable comments if you need any help / information below.


Thank you

0
0 Comments

Featured Post

C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month