VMware {code} Community
ktemplar
Contributor
Contributor

listing of folders and its respective vms

Hi,

I am new at the programming site of VI3. Now I need your help!

How can I realize the relationship between folders and virtual machines, to get a list of folders and vms is no problem. But I need a list of all vms in its respective folders.

It should be looks like that:

Folder1:

vm1,vm3,vm5,...

Folder2:

vm2,vm4,vm6,...

I am waiting for your suggestions. Thanks.

Reply
0 Kudos
15 Replies
cpope
VMware Employee
VMware Employee

I'd like to bump this as I am trying to figure out an easy way to do this as well. Anyone have a good example of how to do this?

Reply
0 Kudos
stumpr
Virtuoso
Virtuoso

I'd have to put some more time into getting the format in a style you want, but something like the attached script can get you started. Essentially you can recursively walk down the vmFolder from each Datacenter and print out the folders and vms. Problem is, a folder can contain another folder, which can contain more folders or vms. So you can get a nested tree.

Another option would be to just list all VMs and use the parent property to get the folder it's currently in....you could build a hash with a key = , then print out a list of VMs from each key. This of course wouldn't maintain the nested folder hierarchy. If you're interested in this option, let me know I can probably work up a quick script demonstrating that method as well.

I included a sample output from the script I attached to this post.

perl ListVMbyFolder.pl --username administrator --password #### --server ####
Datacenter: VMWLAB
  Folder: Discovered Virtual Machine
  Folder: Folder1
    Folder: Folder1.2
      Virtual Machine: VM1.2
    Virtual Machine: AVM
    Virtual Machine: VM1
  Folder: Folder2
    Virtual Machine: VM2

Reuben Stump | http://www.virtuin.com | @ReubenStump
Reply
0 Kudos
cpope
VMware Employee
VMware Employee

Thanks for the response stumpr! You don't happen to have any examples in C# or VB do you? While this perl example looks like it is doing exactly what I want, I am writing a windows GUI program.

This does give me some ideas on how I can achieve this but I was hoping there was a simple way to use a single call to the web services to get the whole tree and not have to go back and make multiple calls.

Thanks!

Reply
0 Kudos
lamw
Community Manager
Community Manager

Hey stumpr,

Great script, hope you don't mind but I thought this might be a good one to add to Code Central under vSphere SDK for Perl (formally VI Perl Toolkit) :

=========================================================================

William Lam

VMware vExpert 2009

VMware ESX/ESXi scripts and resources at:

VMware Code Central - Scripts/Sample code for Developers and Administrators

If you find this information useful, please award points for "correct" or "helpful".

Reply
0 Kudos
stumpr
Virtuoso
Virtuoso

Sorry but my experience is really in C/C++, Perl and Objective-C. I really don't have the confidence to provide any examples in C#. I know someone posted about using VB against the SDK, but I didn't follow it in detail.

Essentially, you just need to replace the Perl Toolkit get_view and find_entity_views with RetrieveProperties calls. I don't think you'll be able to get it to a single call, but if you do your RetrieveProperties call properly, you'll end up with minimal soap return envelopes which would be optimized for speed and efficiency.

Now, if you don't care about the nested folder hierarchy, and just want Folder: Vm, Vm, Vm for example, then you can just do a RetrieveProperties for all VirtualMachines and get their parent property. This should be the Folder they are assigned to. You can then just build a hash map with the Folders as a key and print them. However, this method will still require a RetrieveProperties call to the VIM service to get all the VMs, then you'll still have to do a RetrieveProperties call for each unique parent (Folder) to get its name property. This is because the SDK will return a managed object reference for the parent, it won't provide any properties until you ask for them for each folder.

If I get some time this evening, I can create a perl script that uses RetrieveProperties() and uses this alternate method. This might be easier to convert to C# since the logic flow will be nearly identical, you'd just have to do the C# translation. I've got a few tasks here at work today so it'll have to wait until the evening.

Reuben Stump | http://www.virtuin.com | @ReubenStump
Reply
0 Kudos
stumpr
Virtuoso
Virtuoso

Yeah, no problems there. It's not exactly what I would consider a well documented or clean script, but I'm sure it might help show how to use the child/parent relationships for the objects in the inventory tree.

Thanks for moving it to the Code Central.

Reuben Stump | http://www.virtuin.com | @ReubenStump
Reply
0 Kudos
lamw
Community Manager
Community Manager

np. I think as a code snippet/sample it works well and gives user's an idea on how to get some of this information. If they have questions, they can always elaborate within that thread.

=========================================================================

William Lam

VMware vExpert 2009

VMware ESX/ESXi scripts and resources at:

VMware Code Central - Scripts/Sample code for Developers and Administrators

If you find this information useful, please award points for "correct" or "helpful".

Reply
0 Kudos
cpope
VMware Employee
VMware Employee

Thanks again stumpr for the response. I have done some more digging and figured out that with the retrieveproperties I can actually retrieve the properties of the different types of entities in a single call. Based on the response I can then parse through the returned array looking for the different objects and based on their Parent and Child attributes build the tree. Eventually I'd like to populate a treeview similar to what VC has but with checkboxes for selecting VMs and executing commands on them.

I'll be sure to post back here when I get something working as I'm sure this is a pretty common request.

Reply
0 Kudos
stumpr
Virtuoso
Virtuoso

I'd be curious to see what you come up with. I'm not sure you can get it to a single RetrieveProperties call, since all the parent references are managed object references, which means you'll have to call them again. You might be able to key off the ManagedObjectReference->value property and build your relationships that way to avoid the extra calls.... Like I said, be interested to see what you come up with :).

You can take a look at the View and ViewManager managed objects in the SDK. I haven't spent too much time on it, but they might help build your tree view. From the bit I've seen, looks like you can create a ContainerView from say the Datacenter and get all the objects in it. Not sure if there are any examples of using the Views floating about there, perhaps Ianw might have seen some.

Reuben Stump | http://www.virtuin.com | @ReubenStump
Reply
0 Kudos
cpope
VMware Employee
VMware Employee

Success! One webservice call! see screenshot below. Stumpr, how do you post your code so it stays formatted? I'll post an explanation and a zip later.

5905_5905.PNG

Reply
0 Kudos
lamw
Community Manager
Community Manager

embed your code within the tags

=========================================================================

William Lam

VMware vExpert 2009

VMware ESX/ESXi scripts and resources at:

VMware Code Central - Scripts/Sample code for Developers and Administrators

If you find this information useful, please award points for "correct" or "helpful".

Reply
0 Kudos
stumpr
Virtuoso
Virtuoso

Nice, I'd love to see your code when you're ready to post it. I'm guessing you just got every Datacenter, VM, and Folder object with a RetrieveProperties call and then built your tree? I think the only thing to make sure is you didn't get all properties in the call, just the ones you are looking to make use of in your code. But looks good.

Reuben Stump | http://www.virtuin.com | @ReubenStump
Reply
0 Kudos
red0mark
Enthusiast
Enthusiast

I would be interested in seeing how this is done I've been

working on this very item and I'm making multiple web service call to get the

vms then find the parents.

-Thanks

Reply
0 Kudos
cpope
VMware Employee
VMware Employee

Sorry for the delay everyone in posting this. I have recieved several requests for the code so I have posted the full example VITree project to codecentral. You can find it here:

http://communities.vmware.com/docs/DOC-10891

I'd be interested in hearing how others use this code in their projects!

Curtis

Reply
0 Kudos
PranavS
Contributor
Contributor

A very helpful script again. In-corporated custom changes and working fine! Thanks.

Reply
0 Kudos