errorcode

by antonela at 2013-04-04 00:45:16

in a .cmd I use at the end:

exit /B %ERRORCODE% (where ERRORCODE=%errorlevel%)
In this way I know how my .cmd finishes and I can use this variable for a traffic light in another program.
How could I write something similar in powershell?

I use $LastExitCode to test the errors…
exit /B $LastExitCode doesn’t function
by nohandle at 2013-04-04 07:17:00
I am not sure if you:
1) are trying to replace existing batch file with a powershell script while maintaining the same behavior
2) are trying to detect exit code of a batch file that was run from powershell srcipt

Which one is it?
by antonela at 2013-04-05 02:27:29
the first one.

In my file 1.ps1 I use
sqlcmd -S server -d dbase -b -Q "EXEC pr_MAIN_OLAP"

after this row I put
if ( $LastExitCode -ne 0) { write-host “error!”
exit $LastExitCode
}
exit $LastExitCode

If I run this file (powershell.exe 1.ps1) I’m not sure if using exit $LastExitCode it’s enough to have the exit code I need.
0 -OK
1- Error

What do you think?
by nohandle at 2013-04-05 02:58:20
If you are not sure try it :slight_smile:
Save this in c:\temp\test.ps1 file
sqlcmd give me error
exit $lastExitCode

and from the old command line run:
powershell -file "c:\temp\test.ps1"
echo %errorlevel%

Do you get 1?
by antonela at 2013-04-05 05:27:01
thank you very much