forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstartNativeExecution.ps1
More file actions
49 lines (43 loc) · 1.77 KB
/
Copy pathstartNativeExecution.ps1
File metadata and controls
49 lines (43 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# this function wraps native command Execution
# for more information, read https://mnaoumov.wordpress.com/2015/01/11/execution-of-external-commands-in-powershell-done-right/
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
}
# note, if $ScriptBlock doesn't have a native invocation, $LASTEXITCODE will
# point to the obsolete value
if ($LASTEXITCODE -ne 0 -and -not $IgnoreExitcode) {
if ($VerboseOutputOnError.IsPresent -and $output) {
$output | Out-String | Write-Verbose -Verbose
}
# Get caller location for easier debugging
$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
}
}