Welcome to the Inedo Forums! Check out the Forums Guide for help getting started.
If you are experiencing any issues with the forum software, please visit the Contact Form on our website and let us know!
Otterscript: Usage of Success exit code or EXEC operation - clarifications
-
Hi, I tried to find it in the Forum and documentation but without success.
I have an Otterscript in Buildmaster which should perform different code depending on the success or not of an Exec (or PSExec) command.
How can I do it?
IS there any variable containing the exit code of the command, that I can test in an IF THEN ELSE?
Or does an exit code different than the Success exit code raise an exception and thus I need to put it in a TRY/CATH?
Thanks
-
Hi @fabio-xodo_3872 ,
This is not captured in an output variable, but you can control which exit code means success or failure:
Exec MyProcess.exe ( SuccessExitCode: "> 0" );
Another alternative is to write a PowerShell script that captures the output as a variable, if you need to do logic based on multiple codes.
We could always add support for capturing it as an output variable as well.
hope that helps,
Alana
-
Hi Alana,
Are you planning on adding a return value for an Exec operation? In my job, I call an otter script which calls pwsh via Exec and would like to send an email based on its return (success\failure).
Thanks!
Scott
-
Hi @scusson_9923 ,
Exec
(orExecute-Process
) runs an operating system process, so that's where the return code comes in.If you're doing something like a
PSCall
, then you can create output parameters/variables in the script, and then test those values.-- Dean
-
@dean-houston Hi Dean,
I am having a difficult time understanding the workflow here.
As an experiment, I run a ps1 which sets $Fail to "true" as an Ad Hoc job:
InedoCore::Exec ( FileName: pwsh, WorkingDirectory: C:\, Arguments: test.ps1, ErrorOutputLogLevel: Error, OutputLogLevel: Information, SuccessExitCode: == 0 ); if $Fail == "true" { InedoCore::Send-Email ( ... ); }
I get this: Could not resolve variable $Fail.
Do you have a working example? I must be missing something.
Also, what is the purpose of SuccessExitCode if you cannot test on it?
Thanks!
Scott
-
Hi @scusson_9923 ,
Sorry, I misunderstood; I thought you were doing
PSExec
.In this case you are just executing the
pwsh
process, so you need to figure out how to have that process return an exit code.I don't know if that's the same as
powershell.exe
, but a AI told me "To return an error code from a PowerShell script, theexit
statement followed by the desired error code should be used."So I guess
exit -1
or something like that?-- Dean
-
@dean-houston Hi Dean,
I did try setting the return of my PowerShell test script to exit -1 which fails the InedoCore::Exec call as expected. I just need to capture that failure and test on it.
Thanks,
Scott
-
Any updates on this?
Thanks!
Scott
-
Hi @scusson_9923 ,
Can you clarify what update you're looking for?
Are you looking for help on how to capture a failure and test on it?
Thanks,
Alana
-
Hi Alana,
What I am trying to do is capture the result (success or fail) of this call:
InedoCore::Exec ( FileName: pwsh, WorkingDirectory: C:\, Arguments: test.ps1, ErrorOutputLogLevel: Error, OutputLogLevel: Information, SuccessExitCode: == 0 );
My understanding is that currently InedoCore::Exec does not have a return value. I was hoping for something like:
$ret = InedoCore::Exec ( ... ); if $ret == "0" { // do operations for success } else { // do operations for failure }
Thanks!
Scott
-
Hi @scusson_9923 ,
Thanks for clarifying. You're right, the result is not available as a variable. Instead, the Operation will fail, which means you'd want to handle this via a try/catch.
try { InedoCore::Exec ( FileName: pwsh, WorkingDirectory: C:\, Arguments: test.ps1, ErrorOutputLogLevel: Error, OutputLogLevel: Information, SuccessExitCode: == 0 ); ... operations for success... } catch { ... operations upon failure ... }
Hope that helps,
Alana
-
Hi Alana,
This seems to work for what I am trying to accomplish.
Thanks!
Scott