src/dynamicparams/GetPlasterTemplateVarSet.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
using module .\New-DynamicParam.ps1

function GetPlasterTemplateVarSet {
    [CmdletBinding(PositionalBinding = $true)]
    [OutputType([System.Management.Automation.RuntimeDefinedParameterDictionary])]
    Param(
        [Parameter(Mandatory = $false)]
        [string]$Path,

        [Parameter(Mandatory = $false)]
        [string]$ParameterSetName = 'ByTemplate'
    )
    
    $PlasterTemplateRaw = Get-Content -Path $Path -Raw

    $PlasterParams = [regex]::Matches($PlasterTemplateRaw , "(?<={PLASTER_PARAM_).*?(?=})") | Select-Object -ExpandProperty Value -Unique
    
    $Dictionary = [System.Management.Automation.RuntimeDefinedParameterDictionary]::new()

    # start position at 1; since there Install-Template has a param already at that index
    $PlasterParams | ForEach-Object -Begin {$Position = 1} -Process {
        # the PLASTER_PARAM_Name variable is required, but not used here. its in place to bypass
        # being prompt in the CLI
        if ($_ -ne 'Name') {
            $Position++ 
            New-DynamicParam -DPDictionary $Dictionary -Name $_ -Position $Position -ParameterSetName $ParameterSetName -Mandatory 
        }
    }
    
    $Dictionary
}