8

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:

$result = (start cmd "/c D:\SQL2008R2\SQL2008R2\setup.exe /CONFIGURATIONFILE=sqlconfig.ini && exit 0 || exit 1")

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

2
  • This is not how && works, and you can't choose exit 0 or exit 1 after the setup runs. You have to test ERRORLEVEL, and that's typically done in a batch file, which could then exit with the appropriate error code. Commented Jun 12, 2012 at 20:55
  • Well, it actually does work like that. && runs the left side and if it's false (the command failed) then it runs the right side of the &&. Operator precedence groups (Left && Middle) || Right. Now that I think about it, if || is a conditional-or and only runs until a true value, then I should have actually used & instead of ||. Commented Jun 12, 2012 at 21:07

3 Answers 3

13

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

& cmd.exe /c 'ping.exe doesnotexist && exit 0 || exit 1'

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

Write-Host $LASTEXITCODE

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:

& ping.exe doesnotexist ; Write-Host $LASTEXITCODE

Applied to your command line program:

& cmd.exe /c 'D:\SQL2008R2\SQL2008R2\setup.exe /CONFIGURATIONFILE=sqlconfig.ini && exit 0 || exit 1'

Or just:

& D:\SQL2008R2\SQL2008R2\setup.exe /CONFIGURATIONFILE=sqlconfig.ini

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

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, I saw that you could do that as well. I wanted mine to be in a new window, though. So I needed Start-Process
2

This is how you do it: start is actually an alias Start-Process, so you have to look at it's documentation, which is a lot different than cmd.exe's start. So you can do this:

(Start-Process -FilePath "cmd.exe /c ..." -Wait -Passthru).ExitCode

So easy!

2 Comments

Keep in mind that, that will simply write it to the pipeline. If you want to echo it to StdOut, you would use the Write-Host cmdlet.
This was a false positive. As it turns out, this only works if you remove the start cmd "/c part. It worked for me because I had been trying both in my PowerShell session. Command prompt still isn't returning anything..
0

Too late maybe but does this help?

start /WAIT cmd.exe /C "YOUR-COMMAND-HERE" & if errorlevel 1 echo 'error occurred'

you can also explicitly return an error code like this:

start /WAIT cmd.exe /C "YOUR-COMMAND-HERE & exit MY-ERROR-CODE" & if errorlevel 1 echo 'error occurred'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.