VMware

VMware Tools – VM running on which host?

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:

https://docs.vmware.com/en/VMware-Tools/12.0.0/com.vmware.vsphere.vmwaretools.doc/GUID-D026777B-606D-4442-957A-B953C2049659.html

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

Author

I have more than 13 years experience in the Telco and IT business. Most of this time I spent with VMware environments in the SMB / Enterprise environment. At first I worked in the support department, then I changed to the consulting area and then to the operations and architecture team of a large provider of IT solutions for tax consultants. Currently I work as a Technical Account Manager at VMware. I have an affinity for VDI solutions with a strong technical background in the core product range of our company.

Leave a Reply

Your email address will not be published. Required fields are marked *