One thing that just occurred to me is that this is a function for a single VM. I assume there is a loop somewhere when we need this info for many VMs. The code was inefficient as is for this case, but I think esxcli and the complexity of your API call are quite a bit worse. Can you consider writing a version that can operate on a large list of VMs efficiently?