VMware Cloud Community
TheVMinator
Expert
Expert
Jump to solution

vCO vCheck Output

Has anyone been successful in getting vCO vCheck, developed by jcp0wermac

to send its output as an html file as an email attachment or can you recommend how to do so?

Thanks!

PREVIEW: vCO vCheck

0 Kudos
1 Solution

Accepted Solutions
Burke-
VMware Employee
VMware Employee
Jump to solution

All - I have updated the .package file on the Docs post. See my Comment there for details. HTML formatted e-mail attachment is now implemented :smileycool:

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you!

Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator
for vRealize Orchestrator tips and tutorials - @TechnicalValues on Twitter

View solution in original post

0 Kudos
8 Replies
jcp0wermac
Enthusiast
Enthusiast
Jump to solution

I haven't looked at this in a while.  I would assume you are just using the built in workflow to send email.  Does it fail or you just don't get the message?

Joe

0 Kudos
TheVMinator
Expert
Expert
Jump to solution

OK thanks - the report does run - I used just a simplified version of it that only uses "General INformation".  I added a scriptable task that takes the results attribute as an input parameter.  Then I used some javascript to send an email message containing the results attribute as follows:

var message = new EmailMessage();

var content = "Daily Environment Status Report " + results;

message.smtpHost = "smtp.mycompany.com";

message.toAddress = "myemail@mycompany.com";

message.fromAddress ="mailer@mycompany.com";

message.subject = "Report Results";

message.addMimePart(content,"text/html; charset=UTF-8");

System.log( "sending mail to host: " + message.smtpHost + " with user:" + message.username + ", from:" + message.fromAddress);

message.sendMessage();

The result is that I get an email with the following in the body of the email:

Daily Environment Status Report [ {"title":"General Information","header":"General Information","displayType":"list","comments":"General details on the infrastructure","powerCliAuthor":"Alan Renouf, Frederic Martin","vcoJsAuthor":"Joseph Callen","results":[{"numberOfVms":6},{"numberOfHosts":2},{"numberOfClusters":1},{"numberOfDatastores":3}]}, ]



Which is the information I wanted - I'm getting the information I want, but I need it as an html attachment to the email rather than in the above format which is hard to read

0 Kudos
Burke-
VMware Employee
VMware Employee
Jump to solution

You are actually very close, under your message.addMimePart line and before you send..., add the following:

var reportFile = new MimeAttachment("report.html");

reportFile.content = content; // assign the value of the content variable to the MimeAttachment content -- assuming formatted as HTML

reportFile.mimeType = "text/html"; // Set the mimeType for the file

// now add this mimeAttachment to the e-mail message:

message.addMimePart(reportFile, reportFile.mimeType);

Ta-Da!

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you!

Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator
for vRealize Orchestrator tips and tutorials - @TechnicalValues on Twitter
TheVMinator
Expert
Expert
Jump to solution

OK thanks.  Your code actually did allow me to send the exact same output that was going to the body of the email as an html attachment.  That part of it worked great - thanks.  The only problem is that this is what the webpage looks like:

Daily Environment Status Report [ {"title":"General

Information","header":"General

Information","displayType":"list","comments":"General details on the

infrastructure","powerCliAuthor":"Alan Renouf, Frederic

Martin","vcoJsAuthor":"Joseph

Callen","results":[{"numberOfVms":6},{"numberOfHosts":2},{"numberOfClusters":1},{"numberOfDatastores":3}]},

]

It doesn't have proper formatting.  What I'm doing is taking the output parameter "results" that came from  jcpOwermac 's "Create HTML" scriptable task as part of his workflow, and making in an input parameter for a scriptable task that is designed to take the "results" parameter and send it as an html attachment.

Any ideas on how to get that output to show up correctly?

Thanks!

(Here is the actual JavaScript from jcpOwermac in the "Create HTML" scriptable task:

/* Issue with Configuration.setAttributeWithKey("htmlCode", htmlCodeArray); causes the
* StackOverflowError below.
*
* Modified this to use String[] and reassemble in the Action.
*
* 2013-09-28 01:49:39.085+0000 [WorkflowExecutorPool-Thread-536] WARN  {vcoadmin:Main:22926242-7fc2-4003-b5bf-e70af84c5150:ff808081415be7d8014162409824105e} [DebuggerWorkflowHandler] Error in execution of workflow 'Main for wfExecution[ff808081415be7d8014162409824105e]
* java.lang.StackOverflowError
*       at org.mozilla.javascript.ConsString.appendFragment(ConsString.java:78)
*       at org.mozilla.javascript.ConsString.appendTo(ConsString.java:73)
*       at org.mozilla.javascript.ConsString.appendFragment(ConsString.java:79)
*       at org.mozilla.javascript.ConsString.appendTo(ConsString.java:73)
*       at org.mozilla.javascript.ConsString.appendFragment(ConsString.java:79)
*
*/
var htmlCode = "";
var htmlCodeArray = [];
var resultsObject = JSON.parse(results);

System.log( resultsObject.length );
var loc = resultsObject.length - 1;
for ( ; loc >= 0; loc-- ) {
try {
  System.log( loc );
  var res = resultsObject[loc];
  htmlCode = "";
  if( res == null ) throw "res is null";
  if( res.title == null ) throw "res.title is null";
  if( res.results.length == 0 ) continue;

  htmlCode += "<table><caption>" + res.title +"</caption>";
  /* if its a list there is no "header" */
  if( res.displayType == "table" ) {
   if( res.results.length > 0 ) {
    htmlCode += "<thead><tr>";
    var keys = Object.keys(res.results[0] ); /* these will be the headers */
    for each( var header in keys ) {
     htmlCode += "<th class=\"item\">" + header + "</th>";
    }
    htmlCode += "</tr></thead>";
   }
  }
  htmlCode += "<tbody>";

//htmlCode += "<b>" + p.title +"</b>";
  for each( var wfres in res.results ) {
   var resultKeys = Object.keys(wfres);
   if( resultKeys.length != 0 ) {
    htmlCode += "<tr>";
 
    for each( var rkey in resultKeys ) {
     if( res.displayType == "list" ) {
      htmlCode += "<td>"+ rkey + "</td><td>" + wfres[rkey] + "</td>";
     }
     else {
      htmlCode += "<td>" + wfres[rkey] + "</td>";
     }
    }
    htmlCode += "</tr>";
   }

  }
  htmlCode += "</tbody>";

  /* change colspan to count of keys */
  htmlCode += "<tfoot><tr class=\"comments\"><td colspan=\"5\">" + res.comments + "</td></tr></tfoot></table>";

  htmlCodeArray.push( htmlCode );
}
catch(e) {
  System.error( e );
}
}

try {
System.log("Writting htmlCode...");
System.log(htmlCode.length);
Configuration.setAttributeWithKey("htmlCode", htmlCodeArray);
}
catch(e) {
System.error(e);
}

0 Kudos
Burke-
VMware Employee
VMware Employee
Jump to solution

Please note - I have not imported that package and only am looking at the code you are presenting here in this thread... It seems you have everything you need - the html is being generated in that code you just posted, so you just need to take THAT code and put it into the mimeAttachment rather than the "content" that has the json stuff in it..

As I just noted, I have not run that code, but in looking at what is there, I'm unsure why an array is being used or why the setting of the configuration element is generating an overflow error... I would try getting rid of the array, and instead of pushing each htmlCode chunk into the array, adding it to a String Variable that contains the full html... IE:

var fullHtml = "<html><head></head><body>";

// start loop that generates the htmlCode content...

... do stuff here....

fullHtml += htmlCode;

// end loop here

fullHtml += "</body></html>";

// Now, bind "fullHtml" to an output of the scriptable task and store in Attribute

// bind that fullHtml attribute as an INPUT to your e-mail code scriptable task and use its value as the content of the mimeAttachment...

Give that a try and see how it turns out Smiley Wink

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you!

Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator
for vRealize Orchestrator tips and tutorials - @TechnicalValues on Twitter
jcp0wermac
Enthusiast
Enthusiast
Jump to solution

I don't have my lab environment available in order to review were the JSON to HTML conversion takes place and I can't remember exactly.  I do know that if you change the array to say a string there will be issues.  I kept having exceptions with a single string in the configuration element, at least with all the tests running.  If I have time will I see where I can put an Orchestrator appliance to better answer this question.  I think its an action on the web interface.

I would also assume the formatting will be poor since the CSS won't be available, but maybe since your emailing it that won't be such a concern.

Joe

0 Kudos
TheVMinator
Expert
Expert
Jump to solution

yeah just a basically readable .htm attachment file would be all I need - if you could figure out how to get that to email it would be great - especially for us non-javascript savvy users.  If anyone has a block of javascript I can cut and paste that does it all feel free to share - and specify please which scriptable task it should go in.  I created a new scriptable task after the ones that jcpOwermac had created in his workflow.  Would the javascript go into his scriptable task or into a scriptable task after it that uses the output from his scriptable task as an input parameter?

Thanks again

0 Kudos
Burke-
VMware Employee
VMware Employee
Jump to solution

All - I have updated the .package file on the Docs post. See my Comment there for details. HTML formatted e-mail attachment is now implemented :smileycool:

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you!

Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator
for vRealize Orchestrator tips and tutorials - @TechnicalValues on Twitter
0 Kudos