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

Discuss RoboTask here
Post Reply
juanlu
Posts: 48
Joined: Wed Jul 31, 2019 12:33 am

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

Post 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.
Oleg
Site Admin
Posts: 3014
Joined: Thu Jan 01, 1970 1:00 am
Contact:

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

Post 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.
Oleg Yershov
Oleg
Site Admin
Posts: 3014
Joined: Thu Jan 01, 1970 1:00 am
Contact:

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

Post by Oleg »

Another way:
You can save the output to variable and analyze the output text.
Oleg Yershov
juanlu
Posts: 48
Joined: Wed Jul 31, 2019 12:33 am

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

Post by juanlu »

Yes, that's what I ended up doing, analysing the output. Thanks anyway.
Post Reply