Jeremy_VE
Enthusiast
Enthusiast

Convert strings to Array (Composite Type)

Jump to solution

I'm trying to create a Composite Type array within vCO for the purpose of passing data via the Guest Script Manager package.  I'm using the Guest Script manager to run a PowerShell script that will deploy the 1st domain controller in a new forest.  As of right now I only have 2 customized variables within the script; "$aminPassword" & "$domainName". If I was executing everything directly from vCO I'd have no issue because I could manually select the variables to replace when running the "Run Script in VM Guest" workflow.  Since the process will be initiated from vRA, I have to go through the painstaking process of creating a wrapper.

The workflow within the Guest Script Manager package where this logic is located is called "Run Script in VM Guest".  Within that workflow, there's a scriptable task called "Update Script"  That object has 2 inputs: vcoPath (type:String, attribute) who's value is the path where script is stored on vCO server, and scriptVariables (type: Composite Type(StringtoReplace:string,ReplacingString;string):scriptVariables).  The latter variable would normally be populated during the manual invocation of the workflow when the requester selects the script that they want to have ran.  Since I'm using vRA for the requesting, I have to script the population of the variable based upon string inputs.

The code for the Update Script scriptable task is:

var scriptFile = new FileReader(vcoPath);

scriptFile.open();

var script = scriptFile.readAll();

for each (var scriptVariable in scriptVariables) {

    System.log("Replacing variable " + scriptVariable.get("stringToReplace") + " with value " + scriptVariable.get("replacingString"));

    var search = escapeRegExp(scriptVariable.get("stringToReplace"));

    script = script.replace(new RegExp(search, "g"),scriptVariable.get("replacingString"));

}

//Saving unique file

System.log("Saving script " + vcoPath + " with content : \n" + script);

var fileWriter = new FileWriter(vcoPath);

fileWriter.open();

fileWriter.clean();

fileWriter.write(script);

fileWriter.close();

function escapeRegExp(str) {

  return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");

}

I found the following script online to build the Composite Type array, but it doesn't seem to be working:

scriptVariables = new Array();

var jsObj = { name: "$domainName", value: domainName};

scriptVariables.push(jsObj);

var jsObj1 = { name: "$securePassword", value: somepassword};

scriptVariables.push(jsObj1);

In my solution, $domainName and $securePassword are the variables that are being replaced with the values of the vCO WF variables domainName and somePassword.

The error that I'm getting occurs on the "Update Script" scriptable task stating:

[2015-01-01 03:31:16.420] [I] Replacing variable null with value null

[2015-01-01 03:31:16.420] [I] TypeError: Cannot call method "replace" of null (Workflow:Wrapper Run script in VM guest / Update script (item29)#13)

I put a breakpoint on the scriptable task which holds the code to create the Composite Type array and saw that in fact it was creating an array consisting of 2 columns and 2 rows, but that it was empty.

I've been wracking my brain on this one for a while now and have only found a couple of posts on the web that have been only remotely helpful which brings me finally to my question:  Does anyone know how to resolve this?

v/r

Jeremy

0 Kudos
1 Solution

Accepted Solutions
Jeremy_VE
Enthusiast
Enthusiast

If the "create a dedicated workflow to run the script" section of the tutorial at Leveraging the Guest Script Manager package doesn't work, I will make sure to try your suggestions and provide feedback. If the tutorial does work, I will also provide feedback and let everyone know the solution.

Thanks,

Jeremy

View solution in original post

0 Kudos
4 Replies
iiliev
VMware Employee
VMware Employee

Hi Jeremy,

Just a guess here - looking at the error message, it seems that the content of your script file is not fetched properly into script variable and its value is null, and so calling replace() method on null object fails (as expected).

This could happen if either

(1) your vcoPath attribute's value points to an invalid/non-existing file, or

(2) vcoPath value is correct, but you haven't assigned enough file access permissions to the file its point to (or to its folder).

Could you check whether the value of vcoPath attribute is correct, and also look into js-io-rights.conf config file to check if there are sufficient access permissions given to this file/folder?

For additional info on how to configure file access permissions for Workflows/Javascript code, you can take a look at online documentation available here https://pubs.vmware.com/vsphere-55/index.jsp#com.vmware.vsphere.vco_install_config.doc/GUID-19E74C8D...

-Ilian

0 Kudos
Jeremy_VE
Enthusiast
Enthusiast

If the "create a dedicated workflow to run the script" section of the tutorial at Leveraging the Guest Script Manager package doesn't work, I will make sure to try your suggestions and provide feedback. If the tutorial does work, I will also provide feedback and let everyone know the solution.

Thanks,

Jeremy

0 Kudos
Jeremy_VE
Enthusiast
Enthusiast

I found the action element in the package examples called addScriptVariabletoArrayofScriptVariables And followed the tutorial on the page I listed above. Everything is working flawlessly now.

0 Kudos
pmonk_vmware
VMware Employee
VMware Employee

The action the tutorial speaks about "addScriptVariableToArrayOfScriptVariables" appears to be missing.  So  have attached.

Big thanks to Joerg for emailing it to me 🙂

0 Kudos