VMware {code} Community
Courtney
Enthusiast
Enthusiast
Jump to solution

Using VI perl to get hostname from VM

Hi,

I'm using perl to list all of my vm's in a VC view to gather information about devices (parallel ports, nic, etc). To make reporting easier I need to get what host each VM is associated with. Essentially I'm going backwards as most ppl traverse the datacenter to hosts to get vm's, but I need to know all my machines from within my VC. Any suggestions?

Thanks in advance,

Courtney

0 Kudos
1 Solution

Accepted Solutions
ssurana
VMware Employee
VMware Employee
Jump to solution

Hi Courtney,

You can get the ManagedObjectReference of the HostSystem where the Virtual Machine is running by traversing as follows: VirtualMachine->runtime->host

Then you can refer to the "name" property of the HostSystem object derived from this ManagedObjectReference for the host name where the VM is running.

Note: This property is null if the virtual machine is not running and is not assigned to run on a particular host.

Hope the above helps

~ Sidharth

View solution in original post

0 Kudos
3 Replies
ssurana
VMware Employee
VMware Employee
Jump to solution

Hi Courtney,

You can get the ManagedObjectReference of the HostSystem where the Virtual Machine is running by traversing as follows: VirtualMachine->runtime->host

Then you can refer to the "name" property of the HostSystem object derived from this ManagedObjectReference for the host name where the VM is running.

Note: This property is null if the virtual machine is not running and is not assigned to run on a particular host.

Hope the above helps

~ Sidharth

0 Kudos
Courtney
Enthusiast
Enthusiast
Jump to solution

Hi ssurana,

Thanks for the fast response. Since I'm still learning perl and the VIPERL kit...can you put that in code for me? I'm getting a reference to the hash but then I don't know how to get more information from the MOR.

my $vmhost = $entity_view->runtime->host

returns: ManagedObjectReference=HASH(0x7df5bb0)

Thanks again!!

Courtney

0 Kudos
Courtney
Enthusiast
Enthusiast
Jump to solution

Hi ssurana,

Scratch that last post. Went hunting through some of the example scripts and found what I was looking for! If anyone is interested here's an excerpt:

my $vmhost = $entity_view->runtime->host;

my $vmhostname = Vim::get_view(mo_ref => $vmhost)->name;

print $vmhostname."\n";

It's a tad slow but works nonetheless. Thanks again!

Courtney

0 Kudos