VMware Cloud Community
Vitality1
Contributor
Contributor
Jump to solution

Using .CSV files for PowerShell Scripts in vRO

I am familiar with PowerCLI but not so much vRO yet. My company wants me to use vRO to kick off this script. I know down the road I should convert it to native vRO modules of VMware, and using a .CSV probably not the best thing. But I want to at least get it working for now.

I have put my PowerShell script on the connected PowerShell host. And made an input for a mime attachment. When I run the workflow the output from the PSHost says it doesn't recognize my attachment. Part of the error says:

-csvfile MimeAttachment:ch.dunes.scripting.jsmodel.JSMimeAttachment@2dba85ee

Why is it not my regular .csv file here?

So I guess my question is can vRO put this file on my PSHost to execute the PowerShell script correctly as if I put the file there myself? Ex. ".\Script.ps1 -CSVFile File.csv"

Looking online for this question I have found that I need to do more scripting in vRO/what I'm trying to do is not possible. Any help greatly appreciated.

Reply
0 Kudos
1 Solution

Accepted Solutions
eoinbyrne
Expert
Expert
Jump to solution

Yep, your inputs look fine there.

Actually, I pointed you at a helpful workflow but not the most useful one for what you want. The GSM package achieves a lot of what it does by doing the things like the following

- Permits you to store a script file (or any any file) as a vRO ResourceElement

- Will write that file out to the guest by doing

- Create a temporary file on the vRO server using System.createTempFile(<suffix>);

- Writes the content of the ResourceElement to that temp file

- Copies the temporary file from vRO to the Guest with a defined name and path in the guest FS

- Deletes the temporary file from the vRO server

The screengrab below shows where to find this

pastedImage_0.png

You will need to unpick the code you want but it's mostly there in that section.

Incidentally, I forgot to ask where the CSV content is coming from - is it static or generated somewhere outside vRO and the Guest in question? I ask I forgot to suggest getting something else to write the file onto the PowerShellHost - perhaps via UNC file copy?

View solution in original post

Reply
0 Kudos
6 Replies
eoinbyrne
Expert
Expert
Jump to solution

The MimeAttachment is a JSObject which wraps the CSV file content along with MimeType information about what the content and the entire object is *really* a Java object underneath anyway. When you pass it to the PowerShell invocation like that the vRO system will end up just sending the Java Object type and reference as you've seen.

The important thing to remember here is that the PowerShellHost is an external system to vRO and is accessed remotely so any call to PowerShell which refers to a file will require the file to exist on the filesystem which is local to where it is running. As you've said yourself, this will be a bit more scripting to make the local file.

How big is the CSV file? If it's not too large (< 0.5MB) then you can probably just pass the content as as string to PowerShell? e.g.,

".\Script.ps1 -CSVFileCotnent <CSVFIle content as one long string>"

I've done this before for relatively small amounts of data but did find that PowerShell can be picky about escapes so I just base64 encoded the content and then had my script decode it before use like this - [System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String("$b64EncodedData"));

There is also the GuestScriptManager package which provides support for

- Creating files in the Guest

- Executing a process in the Guest

It depends on the VMware tools APIs but tools will probably be present in the VM anyway

Reply
0 Kudos
Vitality1
Contributor
Contributor
Jump to solution

Thanks for the explanation. The file is max under 5 KB, usually just 1-2 KB.

Passing the content as a string could work I think, would have to rewrite my script a little bit as it's looking for .CSV

The GuestScriptManager package, is there an example out there I can see of using this to put a file on a PSHost?

Reply
0 Kudos
eoinbyrne
Expert
Expert
Jump to solution

The package comes with a workflow which provides that operation - it's a central part of how the whole package works as it needed to solve the same issue you have (get a file into the guest so it can be executed/processed)

Here's a screengrab of the workflow you want

pastedImage_0.png

Reply
0 Kudos
Vitality1
Contributor
Contributor
Jump to solution

Ok I see the package. I made a duplicate of the workflow, and I'm trying to understand this. So I would do something like this for the inputs?

vmUsername: DOMAIN\Username

vmPassword: Password

vm: PowerShell Host

dirPath: C:\

prefix: file

suffix: .csv

Something like this? If so, the part I'm not following is how do I actually get data into C:\file.csv after it's created? Kick off PowerShell script workflow to feed the data back in?

Reply
0 Kudos
eoinbyrne
Expert
Expert
Jump to solution

Yep, your inputs look fine there.

Actually, I pointed you at a helpful workflow but not the most useful one for what you want. The GSM package achieves a lot of what it does by doing the things like the following

- Permits you to store a script file (or any any file) as a vRO ResourceElement

- Will write that file out to the guest by doing

- Create a temporary file on the vRO server using System.createTempFile(<suffix>);

- Writes the content of the ResourceElement to that temp file

- Copies the temporary file from vRO to the Guest with a defined name and path in the guest FS

- Deletes the temporary file from the vRO server

The screengrab below shows where to find this

pastedImage_0.png

You will need to unpick the code you want but it's mostly there in that section.

Incidentally, I forgot to ask where the CSV content is coming from - is it static or generated somewhere outside vRO and the Guest in question? I ask I forgot to suggest getting something else to write the file onto the PowerShellHost - perhaps via UNC file copy?

Reply
0 Kudos
Vitality1
Contributor
Contributor
Jump to solution

Thanks for all your help with this eoinbyrne​. I was able to get a working version of this with your help and some other co-workers. Will update post and mark answer soon.

Reply
0 Kudos