0

首先,正如您可以通过我的代码所说的那样,我不是程序员,也不知道 javascript。我实际上正在使用 VMware Orchestrator(我相信它基于 Rhino javascript 引擎)来创建一些工作流。请详细解释任何答案。

我正在尝试创建一个将在 vCenter 中创建目录树的工作流。

概念:基本上我试图为调用全局树的树的每一层运行嵌套的 for 循环。

问题:第一个 for 循环工作并创建文件夹“Dev”。第二个 for 循环不起作用并出现错误消息:

[2013-06-27 19:16:16.219] [I] Building folder Dev
[2013-06-27 19:16:16.474] [I] Built directory: Dev
[2013-06-27 19:16:16.476] [I] Building folder Servers
[2013-06-27 19:16:16.480] [I] TypeError: Cannot find function createFolder in object Dev. (Workflow:TEST_TNG_New_Site_Folder_Structure / Scriptable task (item2)#36)

我认为这与 JavaScript 无法将全局函数调用到嵌套对象中有关(我不是程序员,也不知道 JavaScript,所以不要在这方面开枪)

代码:(一旦我开始工作就会清理它)

var folderBase = ["Dev","DMZ","Prod","UAT"];

var folderT1 = ["Servers","Workstations"];

var folderT2 = ["Windows","Linux"];

var folderT3 = ["Repo1","Repo2","Repo3","Repo4"];

var folderT4 = ["T1","T2","T3"];



// Create the folderBase level of folders

for (var i=0,l=folderBase.length; i<l; i++) {

    var rootFolder = parentFolder;

    buildFolderTree(folderBase[i]);

        // Create the folderT1 level of folders

        for (var j=0, l=folderT1.length; j<l; j++) {

            var rootFolder = folderBase[i];

            buildFolderTree(folderT1[j]);

                // Create the folderT2 level of folders

                for (var k=0, l=folderT2.length; k<l; k++) {

                    var rootFolder = folderT1[j];

                    buildFolderTree(folderT2[k]);

                        // Create the folderT3 level of folders

                        for (var x=0, l=folderT3.length; x<l; x++) {

                            var rootFolder = folderT2[k];

                            buildFolderTree(folderT3[x]);

                                // Create the folderT4 level of folders

                                for (var y=0, l=folderT4.length; y<l; y++) {

                                    var rootFolder = folderT3[x];

                                    buildFolderTree(folderT4[y]);

                                    }

                            }

                    }       

            }

    }



function buildFolderTree(folderName) {

        System.log("Building folder " + folderName);

        rootFolder.createFolder(folderName);

        System.log("Built directory: " + folderName);

    }
4

1 回答 1

1

这是解决方案。显然它与嵌套的 for 循环和全局对象无关,而更多地与对象引用有关(我仍然不完全理解它)。

//////////////////////////////////////////////////////////////////////////////
// CODE: Javascript                                                         //
// TITLE: BuildFolderTree                                                   //
// AUTHOR: Brandt Winchell                                                  //
// COLLABORATOR: robrtb12                                                   //
// VERSION: 1.0                                                             //
// DATE MODIFIED: June 29, 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 i=0; i<folderBase.length; i++) {
    var newBaseFolder = buildFolderTree(parentFolder, folderBase[i]);
    // Create the $folderT1 level of folders
    for (var j=0; j<folderT1.length; j++) {
        var newT1Folder = buildFolderTree(newBaseFolder, folderT1[j]);
        // Create the $folderT2 level of folders
        for (var k=0; k<folderT2.length; k++) {
            var newT2Folder = buildFolderTree(newT1Folder, folderT2[k]);
            // Create the $folderT3 level of folders
            for (var x=0; x<folderT3.length; x++) {
                var newT3Folder = buildFolderTree(newT2Folder, folderT3[x]);
                // Create the $folderT4 level of folders
                for (var y=0; y<folderT4.length; y++) {
                    var newT4Folder = buildFolderTree(newT3Folder, folderT4[y]);
                    }
                }
            } 
        }
    }
//////////////////////////////////////////////////////////////////////////////
// BUILD FUNCTION $buildFolderTree
function buildFolderTree(parentFolderObj, folderName) {
  System.log("Building folder " + folderName);
  var newFolder = parentFolderObj.createFolder(folderName);
  System.log("Built directory: " + folderName);
  return newFolder;
}
//////////////////////////////////////////////////////////////////////////////
于 2013-07-03T23:50:16.343 回答