From d9e5cf10d8749006ed8a976a67fe2c835487bf11 Mon Sep 17 00:00:00 2001 From: Jared Parsons Date: Fri, 24 Mar 2017 08:16:12 -0700 Subject: [PATCH] Clarify the behavior of Exec command Powershell has two methods of detecting error that have very different semantics: - $?: bool which represents status of last powershell operation - $lastexitcode: integer holding exit value of last windows command When Powershell directly executes a windows command these two variables are roughly interchangable. For example: ``` cmd > & msbuild BadProj > "$?:$lastexitcode" false:1 ``` When Powershell indirectly executes a windows command, say through Invoke-Expression, then the important differences show up. The $? variable will represent Invoke-Expression while $lastexitcode will represent the windows command. ``` cmd > Invoke-Expression "& msbuild BadProj" > "$?:$lastexitcode" true:1 ``` Updated the error checking around Exec to guard against this potentiall difference. --- build/scripts/build-utils.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/scripts/build-utils.ps1 b/build/scripts/build-utils.ps1 index 3d584c5caa4..de1e6030b14 100644 --- a/build/scripts/build-utils.ps1 +++ b/build/scripts/build-utils.ps1 @@ -13,7 +13,7 @@ $ErrorActionPreference="Stop" # Original sample came from: http://jameskovacs.com/2010/02/25/the-exec-problem/ function Exec([scriptblock]$cmd, [string]$errorMessage = "Error executing command: " + $cmd) { $output = & $cmd - if (-not $?) { + if ((-not $?) -or ($lastexitcode -ne 0)) { Write-Host $output throw $errorMessage } -- GitLab