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

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

$
0
0

Hello,

I have been continuing with this workflow to create a VM folder directory in vCenter.  I am having a problem with the function, concerning the:

 

var found = subfolders.indexOf(folderName);        if (found == -1) {            System.log("Building folder " + folderName);            var newFolder = parentFolderObj.createFolder(folderName);            System.log("Built directory: " + folderName);            return newFolder;            }        else {            return;            }        }

 

If the folder does not exist, the script runs as expected and creates the directory correctly.  When the folder does exist, I want it to ignore and skip to the next value.  The error message I receive is:

 

[2013-07-03 18:16:18.782] [I] subfolder Name: Prod
[2013-07-03 18:16:18.784] [I] subfolder Name: UAT
[2013-07-03 18:16:18.786] [I] subfolder Name: DMZ
[2013-07-03 18:16:18.788] [I] subfolder Name: Dev
[2013-07-03 18:16:18.790] [I] allSubFolders array: Prod,UAT,DMZ,Dev
[2013-07-03 18:16:18.792] [I] Folder Dev already exists. Continue with next item
[2013-07-03 18:16:18.794] [I] TypeError: Cannot read property "childEntity" from undefined (Workflow:TEST_TNG_New_Site_Folder_Structure / BuildFolderTree (item2)#54)

 

Here is the complete code:

 

//////////////////////////////////////////////////////////////////////////////
// CODE: Javascript                                                            //
// TITLE: BuildFolderTree                                                    //
// AUTHOR: Brandt Winchell                                                    //
// COLLABORATOR: robrtb12                                                    //
// VERSION: 2.0                                                                //
// DATE MODIFIED: July 3, 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","Prod","UAT"];//base folders
var folderT1 = ["Servers","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 Array();    for (var i in children) {        if (children[i] instanceof VcFolder) {            var subfolderParent = children[i];            var subName = subfolderParent.name; //Get only the folder name            System.log("subfolder Name: " + subName);            allSubFolders.push(subName); //Create an array of all subfolder names            }        }        System.log("allSubFolders array: " + allSubFolders);    //Create folder if the folder does not already exists    var found = allSubFolders.indexOf(folderName); //Does $folderName exists in array.  False = -1        if (found != -1) {            System.log("Folder " + folderName + " already exists. Continue with next item");            return;            }        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 192300

Trending Articles



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