Page 1 of 1

Powershell script errors not raising the error (exit code 0)

Posted: Thu Apr 11, 2024 8:19 pm
by juanlu
Hi Oleg,

I have done some tests, and when a powershell script returns an error, it looks like the PowerShell Script action does not raise the error and the exit code is always 0, no matter what. Specially raising the error would be important in order to use the error-handling.

Thanks, Juanlu.

Re: Powershell script errors not raising the error (exit code 0)

Posted: Fri Apr 12, 2024 8:14 am
by Oleg
This is behavior of Powershell application.
For example the script

Code: Select all

$dest ="C:\dest"
New-Item $dest -type directory -force
$source ="D:\somefile.txt"
Copy-Item $source $dest
always returns exit code 0. It doesn't matter if the file exists or not
If source file does not exist the script print error an message to output, but exit code is 0
You must forcibly change the exit code. For example like this:

Code: Select all

$dest ="C:\dest"
New-Item $dest -type directory -force
$source ="D:\somefile.txt"
Copy-Item $source $dest
# this does not work for some reason 
# exit $LastExitCode
exit $error[0].exception.hresult
If the file exists, 0 is returned. Otherwise, the script returns some integer value.

Re: Powershell script errors not raising the error (exit code 0)

Posted: Fri Apr 12, 2024 8:18 am
by Oleg
Another way:
You can save the output to variable and analyze the output text.

Re: Powershell script errors not raising the error (exit code 0)

Posted: Fri Apr 12, 2024 5:03 pm
by juanlu
Yes, that's what I ended up doing, analysing the output. Thanks anyway.