public/Test-Command.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
<#
    .Synopsis
        Test for the presence of a command.
    .Description
        Test for the presence of a command, function, or executable.
    .Example
        PS C:\> Test-Command -Name packer
        Tests for the presence of "packer" and returns a boolean.
#>

function Test-Command {
    [CmdletBinding()]
    [OutputType([Boolean])]
    param (
        # The name of the command to test.
        [Parameter(Mandatory = $true)]
        [String]
        $Name
    )

    begin {
        Write-LogMessage -Message "Started execution"
    }

    process {
        try {
            Write-LogMessage -Message "Looking for '$Name'."
            Get-Command -Name $Name -ErrorAction Stop | Out-Null
            return $true
        }
        catch {
            Write-LogMessage -Message "Couldn't find '$Name'."
            $global:Error.RemoveAt(0)
            return $false
        }
    }

    end {
        Write-LogMessage -Message "Finished execution"
    }
}