VMware Cloud Community
philk33
Enthusiast
Enthusiast
Jump to solution

Help with handling output for Custom Decision

Hello,

Trying to capture a success or fail output message from and "Invoke External Script" workflow and pass it to a custom decision action. I must not be selecting the correct action in the scripting tab? Please see below.

pastedImage_0.png

Please note the error message in red is unrelated to what I'm asking. My issue is that whether or not the output is success or fail, the custom decision always calls the next workflow. It never goes to sleep upon getting a failure output message.

Script for custom decision is:

if (output = "success") return true

else return false;

Can someone help me correct this? Thank You

0 Kudos
1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

The problem is that Write-Host by default adds a new line character(s) to the text.

So you need to either use Write-Host with the option to not add new line (check https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-host?view=powe...), or change the condition in your custom decision from:

if (output == "success") {

to something like

if (output != null && output.trim() == "success") {

View solution in original post

0 Kudos
11 Replies
poorem
Enthusiast
Enthusiast
Jump to solution

output = "success" is setting the value of output to "success"

output == "success" is testing the value of output against "success"

In your script, output = "success" will always evaluate to true, regardless of the value of output. I'd suggest the following:

if (output == "success") {

     return true;

}

return false;

You don't need the "else".

0 Kudos
philk33
Enthusiast
Enthusiast
Jump to solution

Thank you for the help. It was a good first step. Problem is, now upon success it still fails and goes to sleep:

pastedImage_1.png

Is the powershell attribute not a string?

0 Kudos
poorem
Enthusiast
Enthusiast
Jump to solution

No, it's of type "PowerShellRemotePSObject":

20180245_170259-CapturFiles.png

You could use the getAsJson() method to return the object as a string and then examine it. Maybe change your custom decision to:

var result = output.getAsJson();

System.log("Received response: " + result);

if (result.indexOf("success") != -1) {

     return true;

}

return false;

Whether that works will depend on what object is being returned by the previous workflow. But the addition of the logging should help.

0 Kudos
philk33
Enthusiast
Enthusiast
Jump to solution

I think the success message is not passing through as anything other than null?

pastedImage_0.png

0 Kudos
philk33
Enthusiast
Enthusiast
Jump to solution

Also I was able to get the Guest Script Manager package working for my powershell script.

It does return a string instead of a PSObject. Even though, it still fails with a success message every time.

Clearly there is something wrong with the custom decision workflow?

pastedImage_0.png

0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

Could you show the bindings for 'Invoke an external script' workflow element (to verify that its result is properly bound to 'output' attribute)? Or better, export and attach your whole workflow so we can validate all the bindings.

Also, from the first screenshot it seems you've bound both success and error links from invoke script to custom decision. Could you insert a scriptable task to both of them before they go to custom decision (to validate whether script invocation succeeds or fails)?

0 Kudos
philk33
Enthusiast
Enthusiast
Jump to solution

Here is a copy of the workflow.

0 Kudos
philk33
Enthusiast
Enthusiast
Jump to solution

Here is the powershell in the guest script manager package:

Function Check-Updates
{

$time = [DateTime]::Today.AddDays(-1);
$check = Get-HotFix | sort installedon | select-object -last 1

#$check.InstalledOn | Select-Object -last 1


    If($check.InstalledOn -gt $time)
    {

    Write-Host "success"


    }

    Else
    {

    Write-Host "fail"


    }

};

Check-Updates;

0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

The problem is that Write-Host by default adds a new line character(s) to the text.

So you need to either use Write-Host with the option to not add new line (check https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-host?view=powe...), or change the condition in your custom decision from:

if (output == "success") {

to something like

if (output != null && output.trim() == "success") {

0 Kudos
philk33
Enthusiast
Enthusiast
Jump to solution

It does work, thank you.

I still wish I knew how to script the custom decision to deal with the output from the "Invoke External Script" powershell workflow because it's more reliable than Guest Script Manager Package but it's a good start.

0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

For 'Invoke an external script' workflow to return a valid non-null value, I think you should have a return statement in your Powershell code; something like return "success";

Then, in your custom decision code, you should be able to retrieve this return value by using output.getRootObject()

0 Kudos