VMware Cloud Community
ethanmao200123
Contributor
Contributor
Jump to solution

How to automated select vm

Hi

I try to make a vm name as a string automated input to VC:Virtualmachine as an object to do showing ip address such as vm.summary.guest.ipAddress.

Is there any method to do automated select vmname?

Thanks

Ethan

Reply
0 Kudos
1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

I'm not sure what you mean by 'automated select vm', but if you want co convert a string name (like "myvm") to an instance of VC:VirtualMachine type, you can use either the workflow 'Get virtual machines by name with PC' (available under Library > vCenter > Property collector), or a code like the following:

var all = Server.findAllForType("VC:VirtualMachine", "myvm");

System.log("Found VMs: " + all.length);

for each (var vm in all) {

  System.log(vm.name);

}

A couple of notes:

  • both of these approaches will find VMs whose name contains the passed string as a sub string. That is, it will find not only a VM with name "myvm", but also "myvm2" or "some myvm".
  • you can have more than one VM whose name matches the passed string. So you have to add some code to handle this case.

View solution in original post

2 Replies
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

I'm not sure what you mean by 'automated select vm', but if you want co convert a string name (like "myvm") to an instance of VC:VirtualMachine type, you can use either the workflow 'Get virtual machines by name with PC' (available under Library > vCenter > Property collector), or a code like the following:

var all = Server.findAllForType("VC:VirtualMachine", "myvm");

System.log("Found VMs: " + all.length);

for each (var vm in all) {

  System.log(vm.name);

}

A couple of notes:

  • both of these approaches will find VMs whose name contains the passed string as a sub string. That is, it will find not only a VM with name "myvm", but also "myvm2" or "some myvm".
  • you can have more than one VM whose name matches the passed string. So you have to add some code to handle this case.
ethanmao200123
Contributor
Contributor
Jump to solution

Hi Ilian Iliev

It's helpful to me.

Thanks so much

Ethan

Reply
0 Kudos