Recently a customer asked me if there is an easy way to find out which host a VM is running on. This information should be made available within a Linux VM. And yes, there is a way using PowerCLI and the VMware Tools.
A direct retrieval on which host a VM is running is not possible within the VM via VMware Tools.
However, it is possible to fill a variable within the VM with the value via PowerCLI in the vCenter and then read it out. An easy way to put the variable on all powered on VMs with a PowerCLI script could look like this:
$vms = Get-vm | where { $_.PowerState -eq “PoweredOn”}
foreach ($vm in $vms)
{
$vmname = (Get-VM).Name
$hostname = (Get-VM $vm | get-vmhost).Name
New-AdvancedSetting -Entity $vmname -Name "guestinfo.hypervisor.hostname" -Value $hostname -Confirm:$false -force
}
After that the value of the “guestinfo” variable can be retrieved in the VM, on Windows e.g. with vmtoolsd.exe --cmd "info-get guestinfo.hypervisor.hostname"
For Linux this command is also available, see documentation:
I hope this helps a little further. You could of course restrict the script even further, for example to limit the application to clusters. By the way, the value is discarded after a power cycle of the VM and is then only available again in the VM after the application of the script/command (non persistent setting).
I wonder what we can do with the new vSphere DataSets feature in vSphere 8. Read more about that here: https://core.vmware.com/resource/whats-new-vsphere-8#sec21126-sub6