I was recently compiling a PowerShell script which would be integrated into a SSIS package, depending on the status of the script invocation this would return custom error codes to determine success or failure as part of the package. By default, I would generally use Try/Catch statements and provide and exit code. However, when invoking the script from a SSIS package this would not be returned correctly. The cause of this issue is due the powershell script being launced from the Windows Command Processor, when running natively from Windows PowerShell the exit code was returned successfully.
After a bit of research, I came across the Environment.Exit.Method which provides the functionality to terminate the process and provide the underlying operating system the custom exit code from my script. Basically all this involved is replacing the previous method of providing an exit code with the above .NET method. For Example:
Exit("17");
[Environment]::Exit("17");