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.
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
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") {
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".
Thank you for the help. It was a good first step. Problem is, now upon success it still fails and goes to sleep:
Is the powershell attribute not a string?
No, it's of type "PowerShellRemotePSObject":
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.
I think the success message is not passing through as anything other than null?
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?
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)?
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;
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") {
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.
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()