Bawa
Bawa

Reputation: 95

In powershell, how to return exit code of a subscript to the calling script

Powershell: While calling a subscript from a script, if the subscript is exiting with failure (i.e. exit code not 0), the main script is still exiting with exit code 0, showing as success.

Since subscript command is executed in script there is no error from main script side. How to make main script read the exit code of the subscript to show exit code as failure?

I had tried to return value before exiting from subscript and to call it in main script or using the $lastexitcode value, but those didn't worked.

Main Script:

If filepath is incorrect, then I am getting in output:
(321)
(0)

instead of
(321)
(321)

Upvotes: 4

Views: 2654

Answers (1)

Mohammad Nadeem
Mohammad Nadeem

Reputation: 9392

trap { $host.SetShouldExit(-1) }

Upvotes: 1

Related Questions