Have you tried running the script outside of SAM? What you're describing sounds like it may be a bug in PowerShell when piping output to a file.
PowerShell.exe doesn’t return correct exit codes when using the -File option. | Microsoft Connect
Powershell.exe does not set an exit code when -File is used. | Microsoft Connect