Private/Invoke-NativeCommand.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<#
.SYNOPSIS
    Invokes an arbitrary native command with arguments. If the exit code is not 0
    throws an exception.
 
.EXAMPLE
    Invoke-NativeCommand git status
#>

function Invoke-NativeCommand {
    $cmd = $args[0]
    if ($args.Length -gt 1) {
        $arguments = ($args[1..($args.Length - 1)] | Foreach-Object { "'$_'"}) -join ' '
    } else {
        $arguments = ''
    }
    Invoke-Expression "$cmd $arguments"
    if ($LastExitCode -ne 0) {
        throw "'$cmd $arguments' failed with exit code $LastExitCode"
    }
}