提交 d9e5cf10 编写于 作者: J Jared Parsons 提交者: Jared Parsons

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.
上级 c437379d
......@@ -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
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册