Quantcast
Channel: VMware Communities: Message List
Viewing all articles
Browse latest Browse all 193504

Re: Create a VM directory tree in vCenter (with folder exists check)

$
0
0

I would agree.  I like this solution.

 

Final code for this:

 

//////////////////////////////////////////////////////////////////////////////
// CODE: Javascript                                                            //
// TITLE: BuildFolderTree                                                    //
// AUTHOR: Brandt Winchell                                                    //
// COLLABORATORS: robrtb12, tschoergez                                        //
// VERSION: 2.1.1                                                                //
// DATE MODIFIED: July 5, 2013                                                //
// PURPOSE:  Build a directory tree in vCenter VM & Template section        //
// ADDITIONAL INFO: !!Root folder must be created manually before            //
// running this code!!                                                        //
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// DECLARE VARIABLES
var folderBase = ["Dev","DMZ","Global","Prod","UAT"];//base folders
var folderT1 = ["Servers","Templates","Workstations"]; //sub-folders of $folderBase
var folderT2 = ["Windows","Linux"]; //sub-folders of $folderT1
var folderT3 = ["Repo1","Repo2","Repo3","Repo4"]; //sub-folders of $folderT2
var folderT4 = ["T1","T2","T3"]; //sub-folder of $folderT3
//////////////////////////////////////////////////////////////////////////////
// GLOBAL EXCEPTION CATCH FOR NULL INPUT VARIABLES
if (parentFolder == null) {    throw "REFERENCE ERROR: $parentFolder IS NULL!!";    }
//////////////////////////////////////////////////////////////////////////////
// BUILD THE DIRECTORY TREE
// create the $folderBase level of directory
for (var a=0; a<folderBase.length; a++) {    var newBaseFolder = buildFolderTree(parentFolder, folderBase[a]);    var parentFolderObj = newBaseFolder;      // Create the $folderT1 level of folders    for (var b=0; b<folderT1.length; b++) {           var newT1Folder = buildFolderTree(newBaseFolder, folderT1[b]);        var parentFolderObj = newT1Folder;        // Create the $folderT2 level of folders        for (var c=0; c<folderT2.length; c++) {            var newT2Folder = buildFolderTree(newT1Folder, folderT2[c]);            var parentFolderObj = newT2Folder;            // Create the $folderT3 level of folders              for (var d=0; d<folderT3.length; d++) {                   var newT3Folder = buildFolderTree(newT2Folder, folderT3[d]);                var parentFolderObj = newT3Folder;                // Create the $folderT4 level of folders                for (var e=0; e<folderT4.length; e++) {                     var newT4Folder = buildFolderTree(newT3Folder, folderT4[e]);                    }                  }            }           }    }
//////////////////////////////////////////////////////////////////////////////  
// BUILD FUNCTION $buildFolderTree  
function buildFolderTree(parentFolderObj, folderName) {    //Get a list of sublfolders    var children = parentFolderObj.childEntity;    var allSubFolders = new Properties();    for (var i in children) {        if (children[i] instanceof VcFolder) {            var subfolderParent = children[i];            var subName = subfolderParent.name; //Get only the folder name            if (debugOutput == true) {                System.log("subfolder Name($subName): " + subName);                }            allSubFolders.put(subName,subfolderParent); //Push to array all subfolder names and objects            }        }        if (debugOutput == true) {            System.log("subfolder list($allSubFolders): " + allSubFolders);            }    //Create folder if the folder does not already exists    var found = allSubFolders.keys.indexOf(folderName); //Does $folderName exists in array? True:Index# of subName, False:-1    if (debugOutput == true) {        System.log("Original found($found): " + found);        }        if (found != -1) {            System.log("Folder " + folderName + " already exists. Continue with next item");            return allSubFolders.get(folderName);            }        if (found == -1) {            System.log("Building folder " + folderName);            var newFolder = parentFolderObj.createFolder(folderName);            System.log("Built directory: " + parentFolderObj.name + "/" + folderName);            return newFolder;            }        }
//////////////////////////////////////////////////////////////////////////////

Viewing all articles
Browse latest Browse all 193504

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>