I am trying to find a highly efficient way to get a host's name from a .NET vim.virtualmachine.
I have been successful in getting the MoRef for the VM, then getting all hosts with get-view and simply looping the hosts for one which contains the VM's MoRef but this is highly inefficient when doing it like this for thousands of virtual machines.
I cannot find a property of vim.virtualmachine that contains the ESXi host's name that the VM is running on, nor can I find an efficient function in the documentation.
Note that I am trying to do this as efficiently as possible so I am avoiding the PowerCLI functions where possible and relying on Get-View rather than Get-Host and Get-VM (Although I can use these if there is a smart way to get the information I need efficiently)
Many thanks
So far what I have done it ran a nested for each loop on the output of get-view -viewtype hostsystem with a bunch of filters and properties defined.
Using the loop, I stored the host's .name value against the VMs .MoRef value in a hash table which seems to be created very quickly within the nested for each loops.
Now I use reference the hash table while I loop through my VMs to get their corresponding host.
I'm open to cleaner ideas if anyone has any thoughts?
Would this help.
$VM = get-view –viewtype VirtualMachine –filter @{“Name”=”[vmname]”}
Then
$VM.Summary.Runtime
Should contain the hostname.
Hi,
Not sure if I understood what you need, but let me give a try.
If you are using PowerShell, you can simply call VMHost VM Property and it will return the ESXi object.
Ex: $vm.VMHost
Any other programming language, I guess you can do it through a vim.VirtualMachine object accessing vm_obj.summary.runtime.host or simply vm_obj.runtime.host
Have a look on https://vdc-repo.vmware.com/vmwb-repository/dcr-public/1ef6c336-7bef-477d-b9bb-caa1767d7e30/82521f49... maybe it helps.