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?
How do you call the Powershell Script?
Using the official Powershell Plugin, my PowerSSHell plugin, or an Command.execute()?
Cheers,
Joerg
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 result = oSession.invokeScript('$a = "Hello"; $a ');
...
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 ).
I would like to use the official plugin.
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?
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
Thanks! I recently went with the PowerShell plugin and setting up a PowerShell. Life is now easier : )