VMware Cloud Community
mike-p
Enthusiast
Enthusiast
Jump to solution

Information of connected vCenter servers

I want to get informations about relations of vm's to Hosts, DataCenters , clusters, and vCenters. With get-vm , get-datacenters , get-vmhosts and get-cluster it works fine. Sometimes i am connected to 10 or more vCenter servers. In this case i miss a cmdlet like "get-vcenters". This would be useful to locate an object like vm or host in a large infrastructure.

0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

If I understand it correctly you would like to get the name of the vCenter Server by the virtual machine. You can do this with:

New-VIProperty -Name vCenterServer -ObjectType VirtualMachine `
    -Value {$Args[0].Uid.Split(:)[0].Split(@)[1]} -Force


This extends the VirtualMachineImpl object with the vCenterServer property. Now you can do:

Get-VM | Select-Object -Property Name,vCenterServer

To get the name of the vCenter Server of all virtual machines.

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

Most cmdlets have the -Server parameter where you can specify a specific vCenter.

Get-VM -Server $myVC

You could loop through all connected vCenters like this

$defaultVIServers | %{

   Get-VM -Server $_

}

The variable $defaultVIServers contains all the connected vSphere Servers (vCenter and ESX(i))

The variable $defaultVIServer contains the active connection. See the Connect-VIServer help for more info.

Depending on the Default Server Mode (check with Get-PowerCLIConfiguration), a PowerCLI cmdlet will return results for the active vCenter or for all connected vCenters.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

RvdNieuwendijk
Leadership
Leadership
Jump to solution

If I understand it correctly you would like to get the name of the vCenter Server by the virtual machine. You can do this with:

New-VIProperty -Name vCenterServer -ObjectType VirtualMachine `
    -Value {$Args[0].Uid.Split(:)[0].Split(@)[1]} -Force


This extends the VirtualMachineImpl object with the vCenterServer property. Now you can do:

Get-VM | Select-Object -Property Name,vCenterServer

To get the name of the vCenter Server of all virtual machines.

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Thinking more about your question, I created a function Get-vCenterServer that gets the vCenter Server of a VMware object, or gets a vCenter Server by name. Examples are in the function. If you have imported this function in your PowerCLI session, you can also do "Get-Help Get-vCenterServer -full" to get help about the function.

function Get-vCenterServer {
<#
.SYNOPSIS
Retrieves the vCenter Server by name or by child object.

.DESCRIPTION
Retrieves the vCenter Server by name or by child object. The child object can be passed through the pipeline.

.PARAMETER Name
Specify the name of the vCenter Server you want to retrieve.

.PARAMETER Child
Specify the object of which you want to retrieve the vCenter Server.

.EXAMPLE
Get-vCenterServer -Name vCenter
Retrieves information about the vCenter Server with name vCenter.

.EXAMPLE
Get-vCenterServer -Child (Get-VM MyVM)
Retrieves information about the vCenter Server of the virtual machine MyVM.

.EXAMPLE
Get-VMHost ESX1,ESX2 | Get-vCenterServer
Retrieves information about the vCenter Servers of the hosts ESX1 and ESX2.

.OUTPUTS
VIServerImpl

.COMPONENT
VMware vSphere PowerCLI
#>

  [CmdletBinding(DefaultParameterSetName = "set1")]
  param([Parameter(ParameterSetName="set1")] [string] $Name="*",
        [Parameter(ParameterSetName="set2",ValueFromPipeLine=$true)] $Child)

  process {
    if ($Child) {
      if ($Child.Uid) {
        $Name = $Child.Uid.Split(:)[0].Split(@)[1]
        $DefaultVIServers | Where-Object {$_.Name -like $Name}
      }
    }
    else {
      $DefaultVIServers | Where-Object {$_.Name -like $Name}
    }
  }
}

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos