Search code examples
chocolatey

Can Chocolatey exit with the install/script's exit status?


The command, choco install, doesn't seem to throw exceptions. It returns just an array of text, which isn't the most useful thing. I want to install a list of apps and check for/handle failures. I could parse the log, but it would be nice to handle exceptions as they happen.

Right now I'm doing something like this:

$chocoMsg = (choco install $myApp -y) -join('')

if($chocoMsg -match "install of $myApp was successful.") {
    Write-Host -fo:green 'Success'
} else { 
    # handle errors
}

Is there a better way?


Solution

  • In the most recent release of Chocolatey, 0.9.10, the ability for Chocolatey to exit with the same exit code as the underlying installer was added. You can see this issue that describes this functionality here: https://github.com/chocolatey/choco/issues/512. This should allow you to inspect the returned exit code, and then make a decision about how to proceed.

    Also two bugs came out of this, both corrected in 0.9.10.3: