| |
| |
|
|
| |
| |
| function script:Start-NativeExecution { |
| param( |
| [Alias('sb')] |
| [Parameter(Mandatory=$true)] |
| [scriptblock]$ScriptBlock, |
| [switch]$IgnoreExitcode, |
| [switch]$VerboseOutputOnError |
| ) |
|
|
| $backupEAP = $ErrorActionPreference |
| $ErrorActionPreference = "Continue" |
| Write-Verbose "Executing: $ScriptBlock" |
| try { |
| $cwd = Get-Location |
|
|
| if ($VerboseOutputOnError.IsPresent) { |
| $output = & $ScriptBlock 2>&1 |
| } else { |
| & $ScriptBlock |
| } |
|
|
| |
| |
| if ($LASTEXITCODE -ne 0 -and -not $IgnoreExitcode) { |
| if ($VerboseOutputOnError.IsPresent -and $output) { |
| $output | Out-String | Write-Verbose -Verbose |
| } |
|
|
| |
| $caller = Get-PSCallStack -ErrorAction SilentlyContinue |
| if ($caller) { |
| $callerLocationParts = $caller[1].Location -split ":\s*line\s*" |
| $callerFile = $callerLocationParts[0] |
| $callerLine = $callerLocationParts[1] |
|
|
| $errorMessage = "Execution of {$ScriptBlock} in '$cwd' by ${callerFile}: line $callerLine failed with exit code $LASTEXITCODE" |
| throw $errorMessage |
| } |
| throw "Execution of {$ScriptBlock} in '$cwd' failed with exit code $LASTEXITCODE" |
| } |
| } finally { |
| $ErrorActionPreference = $backupEAP |
| } |
| } |
|
|