VMware Cloud Community
Subnet88
Enthusiast
Enthusiast

Read output of Powershell script as a variable in a Workflow

Is there a way to leverage the output of a powershell script called by VCO as a variable or parameter in another scripting block of a workflow? If so, How?

0 Kudos
6 Replies
tschoergez
Leadership
Leadership

How do you call the Powershell Script?

Using the official Powershell Plugin, my PowerSSHell plugin, or an Command.execute()?

Cheers,

Joerg

0 Kudos
igaydajiev
VMware Employee
VMware Employee

I suppose you are using the official PowerShell plugin.

In this case Yes it is,

You can use the PowerShell:PowerShellRemotePSObject type to preserve the output from PowerShell sript invocation and to pass it from one scripting box to another. One important not is that you will be able to use the output  as long as your powershell session is not closed.

Basicly the steps to follow are:

1. Open PowerShell session (You can use the com.vmware.library.powershell.openSession action)

2. Invoke PowerShell script and preserve the result in attribute of type PowerShell:PowerShellRemotePSObject.

3. Use the attribute as input to some other scripting blocks.

4. close PowerShell session (You can use the com.vmware.library.powershell.closeSession action)

If you are using the scripting API to invoke PowerShell script the steps for getting the output are.

....

var oSession = host.getSession(sessionId)

var result  = oSession.invokeScript('$a = "Hello"; $a ');

var output = result.getResults();

...

where :

     host            - is of type PowerShellHost

     oSession     - is of type PowerShellSession

     result          - is of type PowerShellInvocationResult

     output         - is of type PowerShellRemotePSObject

There is a sample workflow "Toggle virtual machine state" in "Library/PowerShell/Samples" folder that actually passes output from PowerShell script to another scriptiong box..

What actually happens there is.

1. GetVm returns a VM by it's name using pregenerated PowerShell action.

2. The output is than preserved in attribute curVM attribute (of type PowerShellRemotePSObject)

3. poweredOff decision block is invoked recieving as parameter curVM attribute.

4. Based on the state of the curVM another powershell action is invoked (stopVm or StartVM ).

0 Kudos
Subnet88
Enthusiast
Enthusiast

I would like to use the official plugin.

0 Kudos
Bukkit
Contributor
Contributor

Instead of running scripts through the official PowerShell plugin, I use a Command.execute() for the local machine. How may I pass forward a variable in a workflow?

0 Kudos
tschoergez
Leadership
Leadership

Hi!

in case of Command.execute() there is no direct access to objects: In both directions (when calling the script and when getting its results) you only have one string.

So you have to parse that string manually in Javascript. Regular Expressions might help.

For mor complex datatype you might also go for XML: Rewrite the powershell script that it returns XML-data, makes it easier to parse in vCO.

Some links:

http://www.vcoportal.de/2012/07/regular-expression-objects-in-vco-scripts/

http://communities.vmware.com/thread/308300

http://www.vcoportal.de/2011/03/xml-handling-the-e4x-way/

(BTW: that pain to have to serialize inputs and ouptputs of the powershell script was the main reason why the Powershell-Plugins got developed 🙂

Cheers,

Joerg

0 Kudos
Bukkit
Contributor
Contributor

Thanks! I recently went with the PowerShell plugin and setting up a PowerShell. Life is now easier : )

0 Kudos