Public/Variables.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
function Variables {
    [cmdletbinding( DefaultParameterSetName = 'NoName' )]
    param(
        [parameter( ParameterSetName = 'Name',
                    Position = 0,
                    Mandatory = $true)]
        [string]$Name,

        [parameter( ParameterSetName = 'NoName',
                    Position = 0,
                    Mandatory = $true )]
        [System.Collections.Hashtable]$Variables
    )
    if ($PSCmdlet.ParameterSetName -eq 'NoName') {
        $Script:ThisPackerFile.variables = $Variables
    }
    else {
        try {
            $path = Join-Path -Path $Script:ThisPackerInfo.Source -ChildPath "Variables\$Name.json"
    
            if (Test-Path -Path $path) {
                $Script:ThisPackerFile.variables = Get-Content -Path $path | 
                    ConvertFrom-Json |
                    ConvertTo-HashTable
            }
            else {
                throw "Variables: $path - not found!"
            }
        }
        catch {
            throw $PSItem
        }
    }
}