[NoBrainer] Enumerate over all vCenter VMs in vCAC

Did you ever want to quickly enumerate over all vCenters that are connected with vCAC? without storing hard coded credentials? and never worry when endpoints are added or removed? Here is how …

### try {
# Load PowerCLI
Add-PSSnapin VMware.VimAutomation.Core;
# Allow multiple logins
$null = Set-PowerCLIConfiguration -DefaultVIServerMode 
  ([VMware.VimAutomation.ViCore.Types.V1.DefaultVIServerMode]::Multiple) -Confirm:$false
foreach($me in ($m.ManagementEndpoints |? InterfaceType -eq 'vSphere')) {
  # Get DNS name of endpoint
  [Uri] $UriMe = $me.ManagementUri;
  $ViServer = $UriMe.Host;
  # Load credentials of management endpoint
  $cc = $m.LoadProperty($me, 'Credential');
  $Password = [DynamicOps.Common.Utils.ScramblerHelpers]::Unscramble($cc.Password);
  $ViCred = New-Object System.Management.Automation.PSCredential($cc.Username, 
    (ConvertTo-SecureString -String $Password -AsPlainText -Force));
  $null = Connect-ViServer -Server $ViServer -Port $UriMe.Port 
    -Credential $ViCred -ErrorAction SilentlyContinue;
} # foreach
# all logged in vCenters are in $global:DefaultVIServers
$avm = Get-Vm;
Write-Host ("Enumerating all virtual machines SUCCEEDED: '{0}'." -f $avm.Count);
### try

### finally {
# Clean up
foreach($ViServer in $global:DefaultVIServers) {
  $null = Disconnect-ViServer $ViServer.Name -Confirm:$false;
} # foreach
### } # finally

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: