3

I need to get VMs' vCenter information, or at least just the vCenter name,

I've looked online at vmware help and all they have is connect to the vCenter to get the VM info, exactly the opposite of what i'm trying to achieve. Is it even possible ? giving I have the machine fqdn and ipaddress ?

Thanks

DuckStalker
  • 58
  • 1
  • 8
  • Are you asking how to connect to vCenter to get info from the VMs? Or do you need to know from each VM what vCenter they are on? – ojk Oct 24 '14 at 16:14
  • the 2nd way, I know the VMs and their IP and trying to figure out which vCenter they are on. – DuckStalker Oct 24 '14 at 16:56
  • 1
    The Get-VM command has a -server parameter, can't you use this when getting the vm's? Then you would know where they are from :) This parameter takes an array, so you could define all your vCenter servers as an array, and foreach through them using Get-VM -Server. – ojk Oct 24 '14 at 17:00
  • Thanks, yeah this is what i have now, trying to avoid all hardcoding here, but it think this is the way i'm going with, but first i need to know that there is no possible way to find the vCenter of a VM with only name/IP then work on the other way. – DuckStalker Oct 24 '14 at 17:50

2 Answers2

2

The vCenter name sits in the Uid of each object. For a VM, you can try the following snippet:

$vm.Uid.Substring($vm.Uid.IndexOf('@')+1).Split(":")[0]

Or:

Get-VM | % {
  [PSCustomObject] @{
    Name = $_.Name
    vCenter = $_.Uid.Substring($_.Uid.IndexOf('@')+1).Split(":")[0]
  }
}

So this loops through every VM in your connected vCenters, and gets the VM Name, and then pulls the vCenter from the Uid. The format of the Uid appears to be:

/VIServer=[username]@[vCenter]:[port]/VirtualMachine=[VirtualMachineID]

So it starts grabbing the vCenter name at the index of '@' + 1, and then splits the resulting string on a colon (the point before the port) and grabs the first item in that resulting array. This can be done slightly more gracefully with regex:

[regex]::Match($vm.Uid,'.*@(.*):').Captures.Groups[1].Value
omrsafetyo
  • 310
  • 2
  • 7
2

I see the last answer here is 5 years old, but just today I found a way that goes deeper into the object hierarchy without having to deal with strings and splitting. To me it's more like the PoSH way.

(Get-VMHost "MyESXHost").GetClient().config.Server

Here's another way that's less deep in the hierarchy but uses a simple split instead.

(Get-VMHost $ESX).GetClient().ServerUri.Split('@')[1]

On some site I found an example that uses splitting heavier like in the previous answers. I less like this way, especially if it is avoidable, I am for the approach: the simpler, the better, but it works too.

Get-VMHost "MyESXHost" | Select-Object Name,@{N="vCenter";E={$_.Uid.Split('@')[1].Split(':')[0]}}

So, in general, there are three properties (maybe more can be found) that give the same result. :

Uid # need splitting

GetClient().ServerUri # need light splitting

GetClient().config.Server # no splitting, only object properties

I am not going here to looping with ForEach, hope it is not a problem.

  • Hmmm... I don't think this answers the question. You need to know your EsxHost name in your asnwer, while a person asking only knows VM name in a question, and wants to get vCenter name. – Mantvydas Aug 24 '22 at 11:43
  • @Mantvydas Actually, FQDN is not always even fits a VM name. In any case the person have to be connected to vCenter/vCenters for checking vCenter name. If not, it is no way to know something from vCenter side. The solutions can help if somebody is connected to several vCenters and want to know on which vCenter the requested VM is resides. – Vladislav Spector Aug 25 '22 at 12:53