bisysadmin
Contributor
Contributor

Working With Results From Poweshell Generated Action

Jump to solution

Hi there,

I generated an orchestrator action from a powershell script/function I regularly use. The function is supposed to return a System.Object with email contents (toAdd, fromAdd, body, etc) that I would like to use in a scriptable task.

The trouble I am having is that I am unable to access these results in my scriptable task. I know the action returns a PowerShellRemotePSObject (and I have looked at the documentation on "Working With PowerShell Results") but none of my attempts to drill down to the values yield the results I am looking for. I'd really appreciate any help figuring out what I am missing.

Thanks,

0 Kudos
1 Solution

Accepted Solutions
bisysadmin
Contributor
Contributor

Thanks again Ivo. sorry for the delayed feedback. I took a look at the solution you provided but had some errors - there was no function getproperty() defined for the psObject.getRootObject().getProperty('from') call. After fiddling around with the results I found my solution.

I basically looped through the rootobject of the script invocation return value which turns out to be a collection data structure (last result in enumeration italicized/bolded).

.....

var rootObject = psObject.getRootObject

for (item in rootObject)

{

System.log(rootObject[index])

}

[2013-12-10 10:43:33.159] [I] DynamicWrapper (Instance) : [PowerShellPSObject]-[class com.vmware.o11n.plugin.powershell.model.result.PSObject] -- VALUE : com.vmware.o11n.plugin.powershell.model.result.PSObject@e9e592

....

It turns out that the return value in within my Powershell script is the last item in the the collection; from there I was able to retrieve my values:

System.log(rootObj[rootObj.length - 1].getProperty("to"));

System.log(rootObj[rootObj.length - 1].getProperty("from"));

System.log(rootObj[rootObj.length - 1].getProperty("Subject"));

System.log(rootObj[rootObj.length - 1].getProperty("body"));

System.log(rootObj[rootObj.length - 1].getProperty("logFile"));

I'm not sure if this will always be the case with psObject return values from Powershell invoked scripts but more tests will help.

Thanks for your time.

View solution in original post

0 Kudos
4 Replies
igaydajiev
VMware Employee
VMware Employee

Not sure if you already found it but there is a sample workflow demonstrating how to work with PSObjects distributed with the plugin.

It can be found in "Library/PowerShell/Samples/List Directory content".

If still having issue could you provide sample ps script returning the PSObject.

Regards Ivo

0 Kudos
bisysadmin
Contributor
Contributor

Thanks for the response Ivo. I took a look at the sample script and I'm not sure if it applies to my situation. In my case I have a workflow which has actions to do most of what the sample script does see attached image to get an idea - (openssion, add powercli snapin, connect to VI server then call the action that I have generated from the Powershell script and pas the results to LogOutPut scriptable task).

workflow.PNG

The action in question generated from the Powershell script is getOrphanedVMDKfile - sample code of the powershell script is below. I have omitted most of it as it is long and included only the end of the function and what it returns as well as the generated java script invocation.

Thanks

var psScript = ''

psScript +='function Get-Ophaned-VMDK-Files\n';

psScript +='{\n';

psScript +='    If ($countOrphaned -gt 0)\n';

psScript +='    {\n';

psScript +='        $from = "GetOphanedVMDKFiles@" + $strVCName + ".company.com"\n';

psScript +='        $to = "user@address.com\n';

psScript +='        [string]$body = ("Orphaned VMDKs Found: ")\n';

psScript +='        $body += $orphans | Sort-Object -Property Name | ft -AutoSize | out-string\n';

psScript +='        $body += [string]("Total number of orphaned VMDKs: " + $countOrphaned)\n';

psScript +='        $Subject = "Info: VMware orphaned VMDKs"\n';

psScript +='\n';

psScript +='        $message = New-Object  System.Object\n';

psScript +='        $message | Add-Member -type NoteProperty -name from -value $from\n';

psScript +='        $message | Add-Member -type NoteProperty -name to -value $to\n';

psScript +='        $message | Add-Member -type NoteProperty -name body -value $body\n';

psScript +='        $message | Add-Member -type NoteProperty -name Subject -value $Subject\n';

psScript +='        $message | Add-Member -type NoteProperty -name logFile -value $logFile\n';

psScript +='    }    \n';

psScript +='    return $message\n';

psScript +='     \n';

psScript +='}\n';

psScript +='Get-Ophaned-VMDK-Files ' + psServer + ' ' + psDataCenter + ' ' + psCluster + '\n';

return System.getModule("com.vmware.library.powershell").invokeScript( host,psScript,sessionId) ;

0 Kudos
igaydajiev
VMware Employee
VMware Employee

Attached is a sample workflow containing example how can process the output of PS script.

Example contains slightly modified version of workflow generated using "Generate an action from PowerShell script" workflow.

The modification I made are :

1. Add workflow attribute psObject to preserve the output from ps script invocation

2. Added scripting box to print different properties of returned PSObject.

Hope it helps.

0 Kudos
bisysadmin
Contributor
Contributor

Thanks again Ivo. sorry for the delayed feedback. I took a look at the solution you provided but had some errors - there was no function getproperty() defined for the psObject.getRootObject().getProperty('from') call. After fiddling around with the results I found my solution.

I basically looped through the rootobject of the script invocation return value which turns out to be a collection data structure (last result in enumeration italicized/bolded).

.....

var rootObject = psObject.getRootObject

for (item in rootObject)

{

System.log(rootObject[index])

}

[2013-12-10 10:43:33.159] [I] DynamicWrapper (Instance) : [PowerShellPSObject]-[class com.vmware.o11n.plugin.powershell.model.result.PSObject] -- VALUE : com.vmware.o11n.plugin.powershell.model.result.PSObject@e9e592

....

It turns out that the return value in within my Powershell script is the last item in the the collection; from there I was able to retrieve my values:

System.log(rootObj[rootObj.length - 1].getProperty("to"));

System.log(rootObj[rootObj.length - 1].getProperty("from"));

System.log(rootObj[rootObj.length - 1].getProperty("Subject"));

System.log(rootObj[rootObj.length - 1].getProperty("body"));

System.log(rootObj[rootObj.length - 1].getProperty("logFile"));

I'm not sure if this will always be the case with psObject return values from Powershell invoked scripts but more tests will help.

Thanks for your time.

0 Kudos