Public/Terraform/Invoke-TerraformInit.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
56
function Invoke-TerraformInit
{
    [CmdletBinding()]
    param
    (
        # The path to the Terraform config to init
        [Parameter(
            Mandatory = $false,
            Position = 0,
            ValueFromPipelineByPropertyName = $true
        )]
        [ValidateNotNullOrEmpty()]
        [string]
        $TerraformConfigPath = $PWD,

        # The Path to the Terraform binary
        [Parameter(
            Mandatory = $false,
            ValueFromPipelineByPropertyName = $true
        )]
        [ValidateNotNullOrEmpty()]
        [string]
        $TerraformPath = 'terraform',

        # Whether or not to enable color output, defaults to false so as not to break CI/CD tools
        [Parameter(
            Mandatory = $false,
            ValueFromPipelineByPropertyName = $true
        )]
        [bool]
        $EnableColor = $false
    )
    $InitArgs = @('init')
    if (-not $EnableColor)
    {
        $InitArgs += '-no-color'
    }
    try
    {
        $InitParams = @{
            FilePath = $TerraformPath
            ArgumentList = $InitArgs
            WorkingDirectory = $TerraformConfigPath
            SuppressOutput = $true
        }
        if ($VerbosePreference -eq 'Continue')
        {
            $InitParams.Remove('SuppressOutput')
        }
        Invoke-NativeCommand @InitParams
    }
    catch
    {
        Write-Error $_.Exception.Message
    }
}