VMware Cloud Community
thompsoa
Enthusiast
Enthusiast

Export Results to csv

Hi All,

I'm new using vRO and I'd like to modify the following workflow to be able to export the results into a csv file and send it to a shared location.

Thanks a lot!

Alicia Thompson www.bakingclouds.com Twitter:@bakingclouds
Reply
0 Kudos
5 Replies
iiliev
VMware Employee
VMware Employee

Hi,

You can use FileWriter scripting class to achieve this.

So, at the beginning of your scripting code, you need to open the CSV output file with code like:

var csvFile = new FileWriter(System.getTempDirectory() + "/vm_snapshot_report.csv");

csvFile.open();

csvFile.clean();

This will open a file with the given path name (in the above sample, the full file path will be /var/lib/vco/app-server/temp/vm_snapshot_report.csv), and clear its content.

As a second step, add some code to dump the data into this file. In your sample workflow, you have a single System.log() statement inside the loop, that dumps snapshot name and snapshot size on the console. Assuming you want the same information in the CSV file, add the following line just below the System.log():

csvFile.writeLine(snapshot.name + "," + System.formatNumber(totalSize/1024/1024/1024, "0.000"));

Note that if the data you output (snapshot name and snapshot size) may contain comma characters, you may want so surround them with double quotes. something like:

csvFile.writeLine("\"" + snapshot.name + "\",\"" + System.formatNumber(totalSize/1024/1024/1024, "0.000")) + "\"";

And as a last step, at the end you need to close the CSV file:

csvFile.close();

Reply
0 Kudos
swapsweet
Enthusiast
Enthusiast

Hi iiliev ,

Thank you for posting the code below to escape the Comma in the value . This works for single row in CSV . however when I run for multiple rows it breaks , all rows gets appended in single row . 

Any option to start next row for each write operation in CSV file .

 

 

Reply
0 Kudos
swapsweet
Enthusiast
Enthusiast

Hi iiliev ,

Thank you for posting the code below to escape the Comma in the value . This works for single row in CSV . however when I run for multiple rows it breaks , all rows gets appended in single row . 

Any option to start next row for each write operation in CSV file .

 

 

Reply
0 Kudos
eoinbyrne
Expert
Expert

Hi

The issue here is that vRO writes the lines with Unix line breaks ('\n' character). This is built into the FileWriter.writeLine() method.

I suspect you're looking at the CSV output on a Windows OS? One possibility would be to view the content in a Windows editor which can support handling Unix line breaks vs Windows line breaks (btw, these are '\r\n' or two characters)

Alternatively, you could try replacing the calls to writeLine(lineData) with write(lineData + "\\r\\n")

NB: the double back slashes are required there to escape the '\' which JS considers a special character

 

-HTH

 

Reply
0 Kudos
eoinbyrne
Expert
Expert

Or even better, you can set the lineEndType before you use the FileWriter

 

eoinbyrne_0-1658845713290.png

much simpler 🙂