From cfa8e2fd9a0906e014d3e7eeff749e9bc9cd3a77 Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Sun, 24 Apr 2016 18:16:56 -0500 Subject: [PATCH] (GH-707) Exit with specific exit codes When running PowerShell and certain things are found, exit appropriately. - When a file is not found, exit 2. - When a file found is not right, exit 4. - When a file can not be downloaded, exit 404. - When unzipping has issues, exit with its result exit code --- .../helpers/functions/Get-ChocolateyUnzip.ps1 | 1 + src/chocolatey.resources/helpers/functions/Get-WebFile.ps1 | 1 + .../helpers/functions/Start-ChocolateyProcessAsAdmin.ps1 | 6 ++++++ 3 files changed, 8 insertions(+) diff --git a/src/chocolatey.resources/helpers/functions/Get-ChocolateyUnzip.ps1 b/src/chocolatey.resources/helpers/functions/Get-ChocolateyUnzip.ps1 index 47cdbbf028..7e108f4103 100644 --- a/src/chocolatey.resources/helpers/functions/Get-ChocolateyUnzip.ps1 +++ b/src/chocolatey.resources/helpers/functions/Get-ChocolateyUnzip.ps1 @@ -138,6 +138,7 @@ param( Unregister-Event -SourceIdentifier "LogErrors_ChocolateyZipProc" $exitCode = $process.ExitCode + Set-PowerShellExitCode $exitCode $process.Dispose() Write-Debug "Command ['$7zip' $params] exited with `'$exitCode`'." diff --git a/src/chocolatey.resources/helpers/functions/Get-WebFile.ps1 b/src/chocolatey.resources/helpers/functions/Get-WebFile.ps1 index fc2727fddd..05084dd3c6 100644 --- a/src/chocolatey.resources/helpers/functions/Get-WebFile.ps1 +++ b/src/chocolatey.resources/helpers/functions/Get-WebFile.ps1 @@ -222,6 +222,7 @@ param( [GC]::Collect() } + Set-PowerShellExitCode 404 throw "The remote file either doesn't exist, is unauthorized, or is forbidden for url '$url'. $($_.Exception.Message)" } finally { if ($res -ne $null) { diff --git a/src/chocolatey.resources/helpers/functions/Start-ChocolateyProcessAsAdmin.ps1 b/src/chocolatey.resources/helpers/functions/Start-ChocolateyProcessAsAdmin.ps1 index 5296794969..2375df4f70 100644 --- a/src/chocolatey.resources/helpers/functions/Start-ChocolateyProcessAsAdmin.ps1 +++ b/src/chocolatey.resources/helpers/functions/Start-ChocolateyProcessAsAdmin.ps1 @@ -61,9 +61,15 @@ Elevating Permissions and running [`"$exeToRun`" $wrappedStatements]. This may t $exeIsTextFile = [System.IO.Path]::GetFullPath($exeToRun) + ".istext" if (([System.IO.File]::Exists($exeIsTextFile))) { + Set-PowerShellExitCode 4 throw "The file was a text file but is attempting to be run as an executable - '$exeToRun'" } + if (!([System.IO.File]::Exists($exeToRun))) { + Set-PowerShellExitCode 2 + throw "Could not find '$exeToRun'" + } + # Redirecting output slows things down a bit. $writeOutput = { if ($EventArgs.Data -ne $null) {