From 308888eaecef8b0c799a35d4cc19e0b852005921 Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Thu, 16 Jun 2016 17:30:12 -0500 Subject: [PATCH] (GH-802) PowerShell Host - Exit codes should return When running any exit code, ensure that the PowerShell host also receives the error so that it can exit properly. --- .../helpers/chocolateyScriptRunner.ps1 | 4 ++++ .../helpers/functions/Set-PowerShellExitCode.ps1 | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/chocolatey.resources/helpers/chocolateyScriptRunner.ps1 b/src/chocolatey.resources/helpers/chocolateyScriptRunner.ps1 index 5085d69092..5461a05278 100644 --- a/src/chocolatey.resources/helpers/chocolateyScriptRunner.ps1 +++ b/src/chocolatey.resources/helpers/chocolateyScriptRunner.ps1 @@ -58,4 +58,8 @@ if ($env:ChocolateyExitCode -ne $null -and $env:ChocolateyExitCode -ne '') { $exitCode = $env:ChocolateyExitCode } +if ($exitCode -ne 0) { + Set-PowerShellExitCode $exitCode +} + Exit $exitCode \ No newline at end of file diff --git a/src/chocolatey.resources/helpers/functions/Set-PowerShellExitCode.ps1 b/src/chocolatey.resources/helpers/functions/Set-PowerShellExitCode.ps1 index 9d97bfd424..ad360c4d0d 100644 --- a/src/chocolatey.resources/helpers/functions/Set-PowerShellExitCode.ps1 +++ b/src/chocolatey.resources/helpers/functions/Set-PowerShellExitCode.ps1 @@ -45,6 +45,11 @@ param ( [parameter(ValueFromRemainingArguments = $true)][Object[]] $ignoredArguments ) - $host.SetShouldExit($exitCode); + try { + $host.SetShouldExit($exitCode); + } catch { + Write-Warning "Unable to set host exit code" + } + $env:ChocolateyExitCode = $exitCode; }