vRo scriptable task to return the top level folder for a VM

Have you been nesting your virtual machines deep into folders.  If you want to return only the top level folder you can do it with this simple javascript function.  It can also be turned into a action if you would rather:

function return_top_folder(vm)
{
                //get the parent object for vm
                var parent = vm.parent;
                //create array to hold data
                var parentNames = new Array();
                var vmPathName = "";
                //create an array of VcFolder
                while (parent instanceof VcFolder){
                                //push folder names on top of array
                                parentNames.push(parent.name);
                                parent = parent.parent;
                }
                //get length of array
                j = parentNames.length;
                //Identify top level folder
                vmPathName = parentNames[j-2];
                //Identify vm's at root of vCenter
                if (vmPathName == null)
                {
                                vmPathName = "root no folder";
                }
                //Return folder name or root no folder
                return vmPathName;
}

 

 

Execute it by calling :

folder = return_top_folder(vm);

The vm must be an object of VCVirtualMachine.  Enjoy the scriptable task. You can download it here: topfolder

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.