Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

VMware PowerCLI Find vCenter server name of any inventory object

In my daily use of Vmware PowerCLI scripts while pulling inventory from vCenter Server, I always include vCenter Center name in the inventory. For example if I am fetching list of Clusters or VMs with complete information it should include a property name of vCenter Server, where the object belongs. This is very helpful in multiple vCenter Server environment, if later I want to track and work on the vCenter inventory Objects, I can plan precisely. Fetching vCenter and associate the server name with the object is very handy for later reviews or history.

Before starting the script Here first import powershell module VMware.PowerCLI and connect to vCenter server as shown below.

Import-Module VMware.PowerCLI
Exception: The VMware.ImageBuilder module is not currently supported on the core edition of PowerShell

Connect-VIServer -Server marvel.vcloud-lab.com -User Administrator@vsphere.local -Password Computer
Name                      Port      User
----                      ----      ----
marvel.vcloud-lab.com     443       VSPHERE.LOCAL\Administrator

VMware PowerCLI vsphere esxi vcenter connect-viserver server import-module vmware.imagebuilder core edition of powershell find vCenter server name vm cluster datacenter.png

Once logged in successfully and connected to vCenter Server, as in below example I am getting clusters list, in the cluster information vCenter information is located inside ExtensionData >> Client >> ServiceUrl namespace. I am using .net object [System.Uri] to know the host name of vCenter server from given result.

Cluster ExtensionData serviceUrl vim70 vmware.vim.servicecontent systemproxy vmware vsphere vcenter esxi powercli session certificate powershell module install script find vcenter hostname.png

$clusters = Get-Cluster
foreach ($cluster in $clusters)
{
    'Cluster "' + $cluster.name + '" is inside vCenter server "' + ([System.Uri]$cluster.ExtensionData.Client.ServiceUrl).Host + '"'
}

Cluster "Stark_Tower" is inside vCenter server "marvel.vcloud-lab.com"
Cluster "Asgard" is inside vCenter server "marvel.vcloud-lab.com"

There is another way from where you can find and get the vCenter host name from Uid of any object for example Cluster or Virtual Machine. In my case I am getting the vCenter hostname from cluster again from Uid of Get-Cluster cmdlet result property. As this Uid has other information included, it need to be split to get the correct information.

VMware vSphere vCenter vcsa esxi get-cluster get-vm get-vmhost vmware.powercli find vCenter name of object uid split extensiondata clienter serviceurl esxi host cluster.png

$clusters = Get-Cluster
foreach ($cluster in $clusters)
{
    'Cluster "' + $cluster.name + '" is inside vCenter server "' + $cluster.Uid.Split('@')[1].Split(':')[0] + '"'
}

Cluster "Stark_Tower" is inside vCenter server "marvel.vcloud-lab.com"
Cluster "Asgard" is inside vCenter server "marvel.vcloud-lab.com"

Useful Articles
Powercli Get vCenter licenses information
Powercli Get vCenter assigned licenses report
PowerCLI one-liner Reporting, Chaning, Assigning and Removing licenses on ESXi
PowerCLI Get-VMhost The operation on computer failed The WinRM client cannot process the request
PowerCLI Add a SCSI Controller to a Virtual Machine
Powershell vCenter server Rest API create and assign tag and tagcategory
PowerCLI create, modify and assign tag and tagcategory

Go Back

Comment

Blog Search

Page Views

6346644

Follow me on Blogarama