VMware Cloud Community
Eriser04
Contributor
Contributor

Looking for guidance - Trying to get the results from a powershell script using a vRO workflow.

Hello Everyone,

I'm hoping someone here can give me some guidance how to get the results from a powershell script and then pass that data into an output variable.

Here is the code I'm using to execute the powershell script. It works great.

try {

  session = PowerShellHost.openSession();

  var script =  '& "' + PowerShellScript + '" ';

  PowerShellOutput = System.getModule("com.vmware.library.powershell").invokeScript(PowerShellHost,script,session.getSessionId()) ;

} finally {

  if (session){

  PowerShellHost.closeSession(session.getSessionId());

  }

}

Here is an snippet of my powershell script...

Foreach ($ip in $ipAddresses) {

    $pingTest = Test-Connection -ComputerName $ip -Count 1 -Quiet

    if ($pingTest) {

        if (Test-NetConnection -ComputerName $ip -Port $ESXPort -InformationLevel Quiet) {

            #Trigger ESX config

            $Results = $ip

            }

        if (Test-NetConnection -ComputerName $ip -Port $DRACPort -InformationLevel Quiet) {

            #Trigger DRAC config

            $Results = $ip

            }

    }

}

Return $Results

How would I be able to get the output from the powershell script and put that into a variable that I can then use in another workflow? I've seen suggestions like getHostOutput() or getResults(), but I'm failing at getting the syntax right.

Any suggestions would be very helpful.

Thanks,

Tags (2)
1 Reply
iiliev
VMware Employee
VMware Employee

Hi,

invokeScript() action returns an object of type PowerShellRemotePSObject. Check the info for this type in vRO API explorer - it has a method getRootObject() which according to documentation should return result of the corresponding PS script invocation converted to vRO type.

So you can add the following as the last line inside the try{} block

var psresult = PowerShellOutput.getRootObject();

Also documentation says that psresult will be a simple type, ArrayList, Properties, or PowerShellPSObject. Assuming $Results in your PS code is a string it is likely that psresult will be a simple string, which you can pass/assign to some output variable or workflow attribute.

If for some reason psresult above is not a simple string but PowerShellPSObject, you should use one of the methods of PowerShellPSObject type to fetch its value; eg. psresult.getToString() to get it converted to string.