public/Invoke-ExternalCommand.ps1

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
50
51
52
53
54
55
function Invoke-ExternalCommand {
<#
    .SYNOPSIS
    Invokes (executes) an external executable via the command-line
 
    .DESCRIPTION
    Invokes (executes) an external executable via the command-line
 
    .PARAMETER Command
    The command-line or windows executable you wish to execute. Should be a full path to the file if the executable is not in the PATH.
 
    .PARAMETER Arguments
    An array of parameters to the passed on the command-line
 
    .PARAMETER PipeOutNull
    Windows executables are started in thier own process, so we stop this by piping the output to Out-Null;
 
    .EXAMPLE
    Invoke-ExternalCommand -Command bcp.exe -Arguments $myStringArray
 
    Invokes bcp (SQL Bulk Copy) with the parameters stored in $myStringArray.
    Note that the above will only work if bcp.exe is in your PATH. Otherwise, use the full path to bcp.exe
 
    .LINK
    https://github.com/DrJohnT/DeployCube
 
#>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string]$Command,

        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string[]]$Arguments,

        [Parameter(Mandatory = $false)]
        [boolean]$PipeOutNull

    )
    # Reset $LASTEXITCODE in case it was tripped somewhere else
    $Global:LASTEXITCODE = 0;

    # We want the command will write to standard output so we can trace progress
    if ($PipeOutNull) {
        # Piping to Out-Null will stop windows exe programs from forking off in thier own process and allowing powershell to continue
        & $Command $Arguments | Out-Null;
    } else {
        & $Command $Arguments;
    }
    if ($LASTEXITCODE -ne 0) {
        Throw "Error executing $Command";
    }
}