Returning an error code from CMD to Powershell

Question:

I’m working on a Mssql install script and I want to get the results of a silent mssql installation. In my PowerShell script I run this command:

Which should return 0 on fail and 1 on pass. Unfortunately, I don’t get any output back. Any ideas?

Answer:

An alternative to Start-Process is the more syntactically terse call operator &.

The exit code will be contained in the built-in variable $LASTEXITCODE so:

This will contain the exit code of the program run so you don’t necessary have to run it with CMD.exe you could just do:

Applied to your command line program:

Or just:

In both cases $LASTEXITCODE should be 0 for success, non-zero otherwise (if the external program was written correctly).

Source:

Returning an error code from CMD to Powershell by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply